package com.cpop.oam.business.service.impl;

import com.cpop.common.utils.bean.BeanUtils;
import com.cpop.core.base.entity.PageDomain;
import com.cpop.core.base.exception.ServiceException;
import com.cpop.core.utils.SpringUtils;
import com.cpop.core.utils.sql.SqlUtils;
import com.cpop.oam.business.bo.SignGoalBo;
import com.cpop.oam.business.entity.SignArea;
import com.cpop.oam.business.service.SignAreaService;
import com.cpop.oam.business.vo.SignGoalPageVo;
import com.cpop.system.business.vo.StoreRenewPageVo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryColumn;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.cpop.oam.business.entity.SignGoal;
import com.cpop.oam.business.mapper.SignGoalMapper;
import com.cpop.oam.business.service.SignGoalService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import static com.cpop.oam.business.entity.table.SignAreaTableDef.SIGN_AREA;
import static com.cpop.oam.business.entity.table.SignGoalTableDef.SIGN_GOAL;
import static com.cpop.oam.business.entity.table.StaffTableDef.STAFF;
import static com.mybatisflex.core.query.QueryMethods.*;

/**
 * 签约目标表 服务层实现。
 *
 * @author DB
 * @since 2024-02-19
 */
@Service("signGoalService")
public class SignGoalServiceImpl extends ServiceImpl<SignGoalMapper, SignGoal> implements SignGoalService {

    /**
     * 签约指标分页
     * @author DB
     * @since 2024/2/19
     * @return R<Page<SignGoalPageVo>>
     */
    @Override
    public Page<SignGoalPageVo> getSignGoalPage() {
        PageDomain pageDomain = SqlUtils.getInstance().getPageDomain();
        return this.mapper.paginateAs(Page.of(pageDomain.getPageNum(),pageDomain.getPageSize()),
                QueryWrapper.create()
                        .select(SIGN_GOAL.ID,SIGN_GOAL.STAFF_ID,SIGN_GOAL.YEAR,SIGN_GOAL.JAN_GOAL,SIGN_GOAL.JAN_FINISH,SIGN_GOAL.FEB_GOAL,SIGN_GOAL.FEB_FINISH,SIGN_GOAL.MAR_GOAL,SIGN_GOAL.MAR_FINISH,
                                SIGN_GOAL.APR_GOAL,SIGN_GOAL.APR_FINISH,SIGN_GOAL.MAY_GOAL,SIGN_GOAL.MAY_FINISH,SIGN_GOAL.JUN_GOAL,SIGN_GOAL.JUN_FINISH,SIGN_GOAL.JUL_GOAL,
                                SIGN_GOAL.JUL_FINISH,SIGN_GOAL.AUG_GOAL,SIGN_GOAL.AUG_FINISH,SIGN_GOAL.SEP_GOAL,SIGN_GOAL.SEP_FINISH,SIGN_GOAL.OCT_GOAL,SIGN_GOAL.OCT_FINISH,
                                SIGN_GOAL.NOV_GOAL,SIGN_GOAL.NOV_FINISH,SIGN_GOAL.DEC_GOAL,SIGN_GOAL.DEC_FINISH)
                        .select(STAFF.NAME.as(SignGoalPageVo::getStaffName))
                        .from(SIGN_GOAL)
                        .leftJoin(STAFF).on(STAFF.ID.eq(SIGN_GOAL.STAFF_ID)),
                SignGoalPageVo.class,
                //子查询
                item -> item.field(SignGoalPageVo::getSignArea)
                        .queryWrapper(signArea -> queryChain()
                                .select(SIGN_AREA.CITY.as(SignGoalPageVo::getSignArea))
                                .from(SIGN_AREA)
                                .where(SIGN_AREA.SIGN_GOAL_ID.eq(signArea.getId()))));
    }

    /**
     * 新增签约指标
     * @author DB
     * @since 2024/2/19
     * @param bo 请求参数
     */
    @Override
    public void insertSignGoal(SignGoalBo bo) {
        //检查用户是否存在
        int year = LocalDate.now().getYear();
        if (this.count(QueryWrapper.create().where(SIGN_GOAL.YEAR.eq(year))
                .and(SIGN_GOAL.STAFF_ID.eq(bo.getStaffId()))) > 0) {
            throw new ServiceException("该员工已存在签约指标");
        } else {
            SignGoal signGoal = BeanUtils.mapToClass(bo, SignGoal.class);
            signGoal.setYear(year);
            this.save(signGoal);
            //新增签约区域
            List<SignArea> signAreas = new ArrayList<>();
            bo.getSignAreaList().forEach(item->{
                SignArea signArea = new SignArea().setSignGoalId(signGoal.getId()).setCity(item);
                signAreas.add(signArea);
            });
            SpringUtils.getBean(SignAreaService.class).saveBatch(signAreas);
        }
    }

    /**
     * 修改签约指标
     * @author DB
     * @since 2024/2/19
     * @param bo 请求参数
     */
    @Override
    public void updateSignGoal(SignGoalBo bo) {
        //修改签约指标
        SignGoal signGoal = BeanUtils.mapToClass(bo, SignGoal.class);
        this.updateById(signGoal);
        List<SignArea> signAreas = new ArrayList<>();
        bo.getSignAreaList().forEach(item->{
            SignArea signArea = new SignArea().setSignGoalId(signGoal.getId()).setCity(item);
            signAreas.add(signArea);
        });
        SignAreaService signAreaService = SpringUtils.getBean(SignAreaService.class);
        //先删后存
        signAreaService.updateChain().where(SIGN_AREA.SIGN_GOAL_ID.eq(signGoal.getId())).remove();
        signAreaService.saveBatch(signAreas);
    }
}
