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

import com.alibaba.fastjson.JSONObject;
import com.cpop.core.base.entity.PageDomain;
import com.cpop.core.utils.SecurityUtils;
import com.cpop.core.utils.SpringUtils;
import com.cpop.core.utils.sql.SqlUtils;
import com.cpop.oam.business.bo.PutOffAuditBo;
import com.cpop.oam.business.entity.Clue;
import com.cpop.oam.business.entity.ClueRecord;
import com.cpop.oam.business.service.ClueRecordService;
import com.cpop.oam.business.service.ClueService;
import com.cpop.oam.business.vo.CluePageVo;
import com.cpop.oam.business.vo.PutOffAuditPageVo;
import com.cpop.oam.business.vo.TaskArchivingPagVo;
import com.cpop.system.business.bo.StoreRenewBo;
import com.cpop.system.business.service.StoreRenewService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.cpop.oam.business.entity.CluePutOff;
import com.cpop.oam.business.mapper.CluePutOffMapper;
import com.cpop.oam.business.service.CluePutOffService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.cpop.oam.business.entity.table.CluePutOffTableDef.CLUE_PUT_OFF;
import static com.cpop.oam.business.entity.table.ClueRecordTableDef.CLUE_RECORD;
import static com.cpop.oam.business.entity.table.ClueTableDef.CLUE;
import static com.cpop.oam.business.entity.table.StaffTableDef.STAFF;
import static com.cpop.system.business.entity.table.BrandTableDef.BRAND;
import static com.cpop.system.business.entity.table.StoreSignTableDef.STORE_SIGN;
import static com.cpop.system.business.entity.table.StoreTableDef.STORE;
import static com.mybatisflex.core.query.QueryMethods.dateDiff;
import static com.mybatisflex.core.query.QueryMethods.now;

/**
 * 校区延期表 服务层实现。
 *
 * @author DB
 * @since 2024-02-20
 */
@Service("cluePutOffService")
public class CluePutOffServiceImpl extends ServiceImpl<CluePutOffMapper, CluePutOff> implements CluePutOffService {

    /**
     * 延期审核分页
     * @author DB
     * @since 2024/2/21
     * @return Page<PutOffAuditPageVo>
     */
    @Override
    public Page<PutOffAuditPageVo> getPutOffAuditPage() {
        PageDomain pageDomain = SqlUtils.getInstance().getPageDomain();
        return this.mapper.paginateAs(Page.of(pageDomain.getPageNum(), pageDomain.getPageSize()),
                QueryWrapper.create()
                        .select(CLUE_PUT_OFF.ID,CLUE_PUT_OFF.PUT_OFF_STAFF_ID,CLUE_PUT_OFF.PUT_OFF_REASON,CLUE_PUT_OFF.PUT_OFF_FILE_URL,CLUE_PUT_OFF.CREATE_TIME,CLUE_PUT_OFF.PUT_OFF_DATE)
                        .select(CLUE.RECEIPT_TIME,CLUE.STORE_ID,CLUE.CITY)
                        .select(STORE.STORE_NAME,STORE.STORE_ADDR,STORE.PERSON_CHARGE,STORE.PHONE)
                        .select(BRAND.BRAND_NAME)
                        .select(STORE_SIGN.EXPIRE_DATE)
                        // 跟进间隔
                        .select(dateDiff(now(), CLUE.LAST_FOLLOW_UP_TIME).as(CluePageVo::getFollowUpInterval))
                        // 签约时间
                        .select(dateDiff(now(), CLUE.RECEIPT_TIME).as(CluePageVo::getSignInterval))
                        .from(CLUE_PUT_OFF)
                        .leftJoin(CLUE).on(CLUE.ID.eq(CLUE_PUT_OFF.CLUE_ID))
                        .leftJoin(STORE).on(STORE.ID.eq(CLUE.STORE_ID))
                        .leftJoin(BRAND).on(BRAND.ID.eq(STORE.BRAND_ID))
                        .leftJoin(STORE_SIGN).on(STORE_SIGN.STORE_ID.eq(STORE.ID))
                        .where(CLUE_PUT_OFF.AUDIT_STATUS.eq(0)),
                PutOffAuditPageVo.class,
                // 负责人
                item -> item.field(PutOffAuditPageVo::getPutOffStaffName)
                        .queryWrapper(putOffStaff -> queryChain().select(STAFF.NAME.as(PutOffAuditPageVo::getPutOffStaffName))
                                .from(STAFF)
                                .where(STAFF.ID.eq(putOffStaff.getPutOffStaffId()))));
    }

    /**
     * 延期审核选择
     * @author DB
     * @since 2024/2/21
     * @param bo 通过与驳回
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void putOffAuditSelect(PutOffAuditBo bo) {
        JSONObject loginUserInfo = SecurityUtils.getInstance().getLoginUserInfo();
        CluePutOff cluePutOff = this.getById(bo.getId());
        this.updateChain()
                .set(CLUE_PUT_OFF.AUDIT_STATUS, bo.getAuditResult() ? 1 : 2)
                .set(CLUE_PUT_OFF.AUDIT_STAFF_ID,loginUserInfo.getString("id"))
                .where(CLUE_PUT_OFF.ID.eq(bo.getId()))
                .update();
        //添加记录
        ClueRecord clueRecord = new ClueRecord();
        clueRecord.setRecordStaffId(loginUserInfo.getString("id"))
                .setRecordType(7)
                .setRecordContent(bo.getAuditResult() ? "延期通过" : "延期拒绝")
                .setClueId(cluePutOff.getClueId());
        SpringUtils.getBean(ClueRecordService.class).save(clueRecord);
        //通过
        if (bo.getAuditResult()){
            //校区延期
            StoreRenewBo storeRenewBo = new StoreRenewBo();
            Clue clue = SpringUtils.getBean(ClueService.class).getById(cluePutOff.getClueId());
            storeRenewBo.setStoreId(clue.getStoreId())
                    .setRenewDesc(cluePutOff.getPutOffReason())
                    .setRenewType(1)
                    .setRenewDate(cluePutOff.getPutOffDate())
                    .setAnnexUrl(cluePutOff.getPutOffFileUrl());
            SpringUtils.getBean(StoreRenewService.class).storeRenew(storeRenewBo);
        }
    }
}
