package com.app.service.order;

import com.app.constant.OrderConstant;
import com.app.constant.SystemConstant;
import com.app.dto.PageResult;
import com.app.entity.BaseEntity;
import com.app.entity.basics.OpenCourse;
import com.app.entity.basics.YearSemester;
import com.app.entity.guardian.GuardianChild;
import com.app.entity.order.OpenCourseOrderItem;
import com.app.mapper.order.OpenCourseOrderItemMapper;
import com.app.service.basics.OpenCourseService;
import com.app.service.basics.SchoolService;
import com.app.service.basics.YearSemesterService;
import com.app.service.guardian.GuardianChildService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Optional;

@Slf4j
@Service
public class OpenCourseOrderItemService {

    @Autowired
    private OpenCourseOrderItemMapper mapper;

    @Autowired
    private GuardianChildService guardianChildService;

    @Autowired
    private SchoolService schoolService;

    @Autowired
    private OpenCourseService openCourseService;

    @Autowired
    private YearSemesterService schoolYearSemesterService;

    /**
     * 功能描述：查询[订单项]详情
     *
     * @param id 标识
     * @return
     */
    public Optional<OpenCourseOrderItem> findById(String id) {
        return Optional.ofNullable(mapper.selectById(id));
    }

    /**
     * 功能描述：查询[订单项]详情（防止数据越权）
     *
     * @param openCourseOrderId 订单标识
     * @param openCourseId      开课标识
     * @param guardianId         家长标识
     * @return
     */
    public Optional<OpenCourseOrderItem> find(String openCourseOrderId, String openCourseId, String guardianId) {
        return Optional.ofNullable(mapper.selectOne(new QueryWrapper<OpenCourseOrderItem>().lambda()
                .eq(OpenCourseOrderItem::getOpenCourseOrderId, openCourseOrderId)
                .eq(OpenCourseOrderItem::getOpenCourseId, openCourseId)
                .eq(OpenCourseOrderItem::getGuardianId, guardianId)
                .eq(BaseEntity::getIsDel, Boolean.FALSE)
        ));
    }

    /**
     * 功能描述：查询[订单]底下的所有[订单项]
     *
     * @param openCourseOrderId
     * @param guardianId
     * @return
     */
    public List<OpenCourseOrderItem> list(String openCourseOrderId, String guardianId) {
        return mapper.selectList(new QueryWrapper<OpenCourseOrderItem>().lambda()
                .eq(OpenCourseOrderItem::getOpenCourseOrderId, openCourseOrderId)
                .eq(OpenCourseOrderItem::getGuardianId, guardianId)
                .eq(BaseEntity::getIsDel, Boolean.FALSE)
        );
    }

    /**
     * 功能描述：查询[账号]底下[我的课程]
     *
     * @param guardianId
     * @return
     */
    public PageResult<OpenCourseOrderItem> myOpenCourse(String guardianId, Integer page, Integer size) {
        IPage<OpenCourseOrderItem> _page = mapper.myOpenCoursePage(guardianId, OrderConstant.OrderState.SUCCESS_PAY, new Page<>(page, size));
        return new PageResult(page, size, _page.getTotal(), _page.getRecords());
    }

    /**
     * 功能描述：保存/更新[订单项]
     *
     * @param openCourseOrderItem
     */
    public OpenCourseOrderItem saveOrUpdate(OpenCourseOrderItem openCourseOrderItem, String guardianId) {
        OpenCourseOrderItem _openCourseOrderItem;
        if (StringUtils.isEmpty(openCourseOrderItem.getId())) {
            _openCourseOrderItem = new OpenCourseOrderItem();
            _openCourseOrderItem.setGuardianId(guardianId);
            _openCourseOrderItem.setOpenCourseId(openCourseOrderItem.getOpenCourseId());
            _openCourseOrderItem.setOpenCourseOrderId(openCourseOrderItem.getOpenCourseOrderId());

            /* 以下为冗余字段 */
            String organId = SystemConstant.organId;
            YearSemester schoolYearSemester = schoolYearSemesterService.find(organId);
            _openCourseOrderItem.setSchoolYear(schoolYearSemester.getSchoolYear());
            _openCourseOrderItem.setSemester(schoolYearSemester.getSemester());

            Optional<OpenCourse> optionalOpenCourse = openCourseService.findById(openCourseOrderItem.getOpenCourseId());
            if (optionalOpenCourse.isPresent()) {
                OpenCourse openCourse = optionalOpenCourse.get();
                _openCourseOrderItem.setName(openCourse.getName()); // 开课名称
                _openCourseOrderItem.setPrice(openCourse.getPrice()); // 开课价格
                _openCourseOrderItem.setCycle(openCourse.getCycle()); // 星期X
                _openCourseOrderItem.setHours(openCourse.getHours()); // 时间段
            }

            Optional<GuardianChild> optionalAccountChild = guardianChildService.findById(openCourseOrderItem.getGuardianChildId());
            if (optionalAccountChild.isPresent()) {
                GuardianChild guardianChild = optionalAccountChild.get();
                _openCourseOrderItem.setGuardianChildId(openCourseOrderItem.getGuardianChildId());
                _openCourseOrderItem.setChildName(guardianChild.getChildName());
                _openCourseOrderItem.setPhoneNumber(guardianChild.getPhoneNumber());
                _openCourseOrderItem.setGoHome(guardianChild.getGoHome());
                _openCourseOrderItem.setSchoolId(guardianChild.getSchoolId());
                _openCourseOrderItem.setSchoolName(schoolService.getName(guardianChild.getSchoolId()));
                _openCourseOrderItem.setGradeName(guardianChild.getGradeName());
                _openCourseOrderItem.setClassName(guardianChild.getClassName());
            }
            _openCourseOrderItem.setState(openCourseOrderItem.getState());
            _openCourseOrderItem.setIsDel(openCourseOrderItem.getIsDel());
            mapper.insert(_openCourseOrderItem);
        } else {
            _openCourseOrderItem = mapper.selectById(openCourseOrderItem.getId());
            _openCourseOrderItem.setState(openCourseOrderItem.getState());
            _openCourseOrderItem.setIsDel(openCourseOrderItem.getIsDel());
            mapper.updateById(_openCourseOrderItem);
        }

        return _openCourseOrderItem;
    }

}
