package com.dk.mdm.service.mst;

import com.dk.common.infrastructure.annotaiton.Pagination;
import com.dk.common.infrastructure.constant.Constant;
import com.dk.common.infrastructure.xxl.XxlJobUtils;
import com.dk.common.model.pojo.PageList;
import com.dk.common.response.ResponseResultUtil;
import com.dk.common.response.ResponseResultVO;
import com.dk.mdm.infrastructure.convert.mst.FollowPlanConvert;
import com.dk.mdm.infrastructure.util.AuthUtils;
import com.dk.mdm.mapper.mst.CustomerMapper;
import com.dk.mdm.model.pojo.mst.Customer;
import com.dk.mdm.model.pojo.mst.FollowPlan;
import com.dk.mdm.mapper.mst.FollowPlanMapper;
import com.dk.common.service.BaseService;
import com.dk.common.mapper.BaseMapper;
import com.dk.mdm.model.query.mst.FollowPlanQuery;
import com.dk.mdm.model.response.mst.FollowPlanResponse;
import com.dk.mdm.model.vo.mst.FollowPlanVO;
import com.dk.mdm.service.common.CommonService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class FollowPlanService extends BaseService<FollowPlan> {

    @Override
    public BaseMapper<FollowPlan> getRepository() {
        return followPlanMapper;
    }

    @Autowired
    private FollowPlanMapper followPlanMapper;

    @Autowired
    private XxlJobUtils xxlJobUtils;

    @Autowired
    private FollowPlanConvert followPlanConvert;

    @Autowired
    private CommonService commonService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private AuthUtils authUtils;

    @Autowired
    private CustomerMapper customerMapper;

    /**
     * @desc : 条件查询
     * @author : 于继渤
     * @date : 2024/7/09 8:45
     */
    @Pagination
    public ResponseResultVO<PageList<FollowPlanResponse>> selectByCond(FollowPlanQuery followPlanQuery) {
        return super.mergeListWithCount(followPlanQuery, followPlanMapper.selectByCond(followPlanQuery),
                followPlanMapper.countByCond(followPlanQuery));
    }


    /**
     * @desc : 新建
     * @author : 于继渤
     * @date : 2024/7/9 8:45
     */
    @Transactional(
            rollbackFor = {Exception.class}
    )
    public ResponseResultVO<?> insert(FollowPlanVO followPlanVO) {
        FollowPlan followPlan = followPlanConvert.convertToPo(followPlanVO);
        //设置ID
        Map<String, Object> uniqueNoteCode = commonService.getUniqueNoteCode(Constant.docNameConstant.FOLLOWPLAN.getName(), true);
        followPlan.setPlanId(uniqueNoteCode.get("outId").toString());
        //查询客户信息
        Customer customer = customerMapper.selectById(new Customer().setCusId(followPlanVO.getCusId()));

        Customer customerSave = new Customer();
        if (Constant.saleCustomerStatusConstant.SALE_STATUS_INTE.getName().equals(followPlanVO.getSaleStatus())) {
            customerSave.setSaleStatus(Constant.saleCustomerStatusConstant.SALE_STATUS_POTE.getName());
            //当前客户部门是否为空
            if (customer.getOrgId() == null) {
                customerSave.setOrgId(authUtils.getStaff().getOrgId());
            }
            List<Long> wxUsers = new ArrayList<>();
            List arrList = new ArrayList(wxUsers);
            arrList.add(authUtils.getStaff().getStaffId());
            customerSave.setFollowStaffs(arrList);
            //更新客户信息
            customerService.updateByUuid(customerSave);
        }

        //设置ID
        String nextId = uniqueNoteCode.get("outId").toString();
        int id = xxlJobUtils.create(Math.toIntExact(
                (followPlanVO.getNextFollowTime().toInstant(ZoneOffset.of("+8")).toEpochMilli() - System.currentTimeMillis()) / 1000
        ), nextId, Constant.XxlJobInfo.FOLLOW.getValue());
        followPlan.setXxlJobId(id);
        followPlanMapper.insert(followPlan);
        return ResponseResultUtil.success(followPlan);
    }
}
