package com.dayou.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dayou.common.BaseEntity;
import com.dayou.dto.CommissionDeclareDTO;
import com.dayou.dto.UserShareRate;
import com.dayou.dto.WorkNodeCommit;
import com.dayou.entity.*;
import com.dayou.enums.FinanceInvoiceState;
import com.dayou.enums.MainBusinessEnum;
import com.dayou.enums.UserTypeEnum;
import com.dayou.mapper.CommissionDeclareMapper;
import com.dayou.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayou.service.workflow.IWorkFlowService;
import com.dayou.utils.LoginContext;
import com.dayou.vo.CommissionDeclareDetailVO;
import com.dayou.vo.CommissionDeclareVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


/**
 * <p>
 * 提成申报 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2024-05-15
 */
@Service
public class CommissionDeclareServiceImpl extends ServiceImpl<CommissionDeclareMapper, CommissionDeclare> implements ICommissionDeclareService {


    @Autowired
    private CommissionDeclareMapper commissionDeclareMapper;

    @Autowired
    private IMajorProductionAllotService majorProductionAllotService;

    @Autowired
    private IWorkFlowService workFlowService;

    @Autowired
    private IBusinessCommissionRateService commissionRateService;

    @Autowired
    private IMajorProductionService majorProductionService;

    @Autowired
    private IMajorService majorService;



    @Override
    public Boolean delete(Long id){
        //逻辑删除
        return this.removeById(id);
    }

    @Override
    public Page<CommissionDeclareVO> majorMarketDeclarePage(Page page, CommissionDeclareVO commissionDeclare) {
        Long currentMarketUserId = LoginContext.getCurrentUserId();
        return commissionDeclareMapper.majorMarketDeclarePage(page,commissionDeclare,currentMarketUserId);
    }

    @Override
    public Page<CommissionDeclareVO> majorEvaluateDeclarePage(Page page, CommissionDeclareVO commissionDeclare) {
        Long currentEvaluateUserId = LoginContext.getCurrentUserId();
        return commissionDeclareMapper.majorEvaluateDeclarePage(page,commissionDeclare,currentEvaluateUserId);
    }

    @Override
    @Transactional
    public Boolean commissionDeclare(CommissionDeclareDTO declareDTO) {
        //保存申报记录
        CommissionDeclare commissionDeclare = BeanUtil.copyProperties(declareDTO, CommissionDeclare.class);
        commissionDeclare.setDeclareResult(FinanceInvoiceState.审核中.name());
        commissionDeclare.setDeclareUserId(LoginContext.getCurrentUserId());
        this.save(commissionDeclare);
        //设置提成比例
        List<UserShareRate> userShareRates = declareDTO.getUserShareRates();
        List<MajorProductionAllot> collect = userShareRates.stream().map(x -> {
            MajorProductionAllot majorProductionAllot = new MajorProductionAllot();
            majorProductionAllot.setMajorId(declareDTO.getBusinessId());
            majorProductionAllot.setMajorProductionId(declareDTO.getProductionId());
            majorProductionAllot.setUserId(x.getUserId());
            majorProductionAllot.setRatio(x.getRate());
            majorProductionAllot.setUserType(UserTypeEnum.MARKET.name());
            majorProductionAllot.setDeclareId(commissionDeclare.getId());
            return majorProductionAllot;
        }).collect(Collectors.toList());
        majorProductionAllotService.saveBatch(collect);
        //提交审核流程
        return workFlowService.openingDeclare(MainBusinessEnum.COMMISSION_DECLARE_MAJOR_MARKET, commissionDeclare.getId());
    }

    @Override
    public CommissionDeclareDetailVO detail(Long id) {
        CommissionDeclareDetailVO commissionDeclareDetailVO = new CommissionDeclareDetailVO();
        CommissionDeclareVO declare = commissionDeclareMapper.detail(id);
        BeanUtil.copyProperties(declare,commissionDeclareDetailVO);
        if (declare.getBusinessType().equals(MainBusinessEnum.MAJOR_BUSINESS.name())){
            if (declare.getBusinessId()!=null){
                Major order = majorService.getOne(new LambdaQueryWrapper<Major>().select(Major::getOrderId, Major::getName).eq(BaseEntity::getId, declare.getBusinessId()).eq(BaseEntity::getDeleted, Boolean.FALSE));
                commissionDeclareDetailVO.setOrderId(order.getOrderId());
                commissionDeclareDetailVO.setName(order.getName());
            }
            if (declare.getProductionId()!=null){
                MajorProduction production = majorProductionService.getOne(new LambdaQueryWrapper<MajorProduction>()
                        .select(MajorProduction::getReportNo, MajorProduction::getProduction, MajorProduction::getName).eq(BaseEntity::getId, declare.getProductionId())
                        .eq(BaseEntity::getDeleted, Boolean.FALSE));
                commissionDeclareDetailVO.setName(production.getName());
                commissionDeclareDetailVO.setReportNo(production.getReportNo());
                commissionDeclareDetailVO.setProduction(production.getProduction());
            }
            List<UserShareRate> rates = majorProductionAllotService.allotRateByDeclareId(id);
            commissionDeclareDetailVO.setUserShareRates(rates);
        }

        return commissionDeclareDetailVO;
    }
}
