package com.goafanti.order.service.impl;

import com.goafanti.business.bo.BusinessProjectBo;
import com.goafanti.common.dao.*;
import com.goafanti.common.model.*;
import com.goafanti.order.bo.InputAddTask;
import com.goafanti.order.bo.InputSonProject;
import com.goafanti.order.enums.NewProjectTypeEnum;
import com.goafanti.order.service.OrderProjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
//@Async
public class ProjectAsyncService {
    @Resource
    private BusinessProjectMapper businessProjectMapper;
    @Resource
    private OrderProjectService orderProjectService;
    @Resource
    private TTaskMemberMapper tTaskMemberMapper;
    @Resource
    private TOrderTaskMapper tOrderTaskMapper;
    @Resource
    private BusinessCategoryMapper businessCategoryMapper;
    @Resource
    private TOrderMidMapper tOrderMidMapper;
    @Resource
    private TTaskMidMapper tTaskMidMapper;

    public void pushFinanceOrTechnology(InputAddTask t) {
        BusinessProject bp=businessProjectMapper.selectById(t.getCommodityId());
        if (bp.getProjectType().equals(NewProjectTypeEnum.HT.getCode())){
            InputAddTask t2=new InputAddTask();
            BeanUtils.copyProperties(t,t2);
            //先新增子项会员
            addSonProject(t);
            //处理主项目本次派单年份
            tTaskMemberMapper.updateServiceYearBySplitSuper(t.getId());
            //会员项目替换
            addFinanceOrTechnology(t2);
        }
    }

    private void addSonProject(InputAddTask t) {
        List<InputAddTask> sonList = t.getSonList();
        if (sonList != null && !sonList.isEmpty()){
            for (InputAddTask e : sonList) {
                InputSonProject sonProject= new InputSonProject();
                sonProject.setTid(t.getId());
                sonProject.setServiceYear(e.getServiceYear());
                sonProject.setTaskComment(e.getTaskComment());
                sonProject.setHtMember(e.getHtMember());
                sonProject.setAdditionalDeduction(e.getAdditionalDeduction());
                sonProject.setRdAwardsubsidy(e.getRdAwardsubsidy());
                orderProjectService.addMemberSonProject(sonProject);
            }
        }

    }


    /**
     *
     * @param t2 项目情况
     */
    private void addFinanceOrTechnology(InputAddTask t2) {
        List<BusinessProjectBo> additionalDeductionList = businessProjectMapper.selectByFirstBName(null, 1);
        for (BusinessProjectBo e : additionalDeductionList) {
            if (!e.getId().equals(t2.getCommodityId())) {
                InputAddTask t=new InputAddTask();
                BeanUtils.copyProperties(t2,t);
                t.setCommodityId(e.getId());
                t.setCommodityName(e.getBname());
                t.setMain(0);
                t.setCommodityPrice(new BigDecimal(0));
                t.setId(null);
                addTTaskMember(t);
                addTaskMid(t);
                addSonProject(t);
                //处理主项目本次派单年份
                tTaskMemberMapper.updateServiceYearBySplitSuper(t.getId());
            }

        }
    }

    public int addTaskMid(InputAddTask t) {
        TTaskMid tm=new TTaskMid();
        tm.setTid(t.getId());
        //判断这个项目是不是高新复审
        int highNewRetrial=0;
        BusinessProject b=businessProjectMapper.selectById(t.getCommodityId());
        if (b.getType()==5){
            int i=tOrderTaskMapper.getHighNewRetrial(t.getOrderNo());
            if (i>0) {
                highNewRetrial =1;
            }
            BusinessCategory bc =businessCategoryMapper.selectByPrimaryKey(b.getCid());
            if (bc !=null && bc.getSort().equals(6)){
                TOrderMid tom=new TOrderMid();
                tom.setOrderNo(t.getOrderNo());
                tom.setProjectType(1);
                tOrderMidMapper.updateByOrderNo(tom);
            }
        }
        tm.setHighNewRetrial(highNewRetrial);
        tm.setLastYear(t.getLastYear());
        tm.setLastYearCapital(t.getLastYearCapital());
        tm.setLastYearIncome(t.getLastYearIncome());
        //获取派单省份与部门
        TTaskMid dtails = tTaskMidMapper.getDtails(t.getId());
        tm.setDispatchProvince(dtails.getDispatchProvince());
        tm.setDutyDep(dtails.getDutyDep());
        tm.setChange(t.getChange());
        return tTaskMidMapper.insertSelective(tm);
    }

    private void addTTaskMember(InputAddTask t) {
        String str=tOrderTaskMapper.selectByidGetCname(t.getCommodityId());
        t.setCname(str);
        tOrderTaskMapper.insertSelective(t);
        addTTaskMember(t.getId(),t.getServiceLife(),t.getServiceYear(),t.getYearSum(),t.getContractTerm());
    }

    public void addTTaskMember(Integer id, String serviceLife, String serviceYear, Integer yearSum, String contractTerm) {
        if (serviceLife!=null|| serviceYear!=null||yearSum!=null|| contractTerm!=null){
            TTaskMember ttm=new TTaskMember();
            ttm.setTid(id);
            if(serviceLife!=null)ttm.setServiceLife(serviceLife);
            if(serviceYear!=null)ttm.setServiceYear(serviceYear);
            if(yearSum!=null)ttm.setYearSum(yearSum);
            if(contractTerm!=null)ttm.setContractTerm(contractTerm);
            tTaskMemberMapper.insertSelective(ttm);
        }
    }
}
