package com.goafanti.techproject.service.impl;

import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.dao.*;
import com.goafanti.common.model.*;
import com.goafanti.common.utils.DateUtils;
import com.goafanti.common.utils.StringUtils;
import com.goafanti.common.utils.excel.NewExcelUtil;
import com.goafanti.core.mybatis.BaseMybatisDao;
import com.goafanti.core.mybatis.page.Pagination;
import com.goafanti.core.shiro.token.TokenManager;
import com.goafanti.order.bo.TOrderTaskBo;
import com.goafanti.techproject.bo.InputTaskDetailsBo;
import com.goafanti.techproject.bo.InputTaskFeature;
import com.goafanti.techproject.bo.TaskDetailsBo;
import com.goafanti.techproject.service.TaskDetailsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 项目申报详情(TaskDetails)表服务实现类
 *
 * @author makejava
 * @since 2024-12-26 17:11:50
 */
@Service("taskDetailsService")
public class TaskDetailsServiceImpl extends BaseMybatisDao<TaskDetailsMapper> implements TaskDetailsService {
    @Resource
    private TaskDetailsMapper taskDetailsMapper;

    @Resource
    private TaskDetailsLogMapper taskDetailsLogMapper;

    @Resource
    private TOrderNewMapper tOrderNewMapper;
    @Resource
    private TOrderMidMapper tOrderMidMapper;
    @Resource
    private TOrderTaskMapper tOrderTaskMapper;
    @Resource
    private TaskFeatureMapper taskFeatureMapper;
    @Resource
    private TaskFeatureLogMapper taskFeatureLogMapper;
    @Resource
    private TTaskMemberMapper ttaskMemberMapper;



    @Value(value = "${upload.path}")
    private final String	uploadPath			= null;


    @Override
    public Pagination<TaskDetails> list(TaskDetails taskDetails, Integer pageNo, Integer pageSize) {
        Map<String, Object> params = new HashMap<>();
        params.put("in", taskDetails);
        return (Pagination<TaskDetails>) findPage("findTaskDetailsList",
                "findTaskDetailsCount", params, pageNo, pageSize);
    }

    @Override
    public TaskDetailsBo pushByTid(Integer id) {
        TaskDetailsBo res = new TaskDetailsBo();
        res.setUpdateStatus(0);
        TOrderTask tOrderTask = tOrderTaskMapper.queryById(id);
        TaskDetails taskDetails=taskDetailsMapper.selectById(tOrderTask.getProgressTid());
        if (taskDetails !=null){
            BeanUtils.copyProperties( taskDetails,res);
            if (res.getProjectId()!=null&&res.getProjectId().equals(TokenManager.getAdminId())){
                res.setUpdateStatus(1);
            }
        }else {
            res.setTid(id);
        }
        addOrderInformation(res);
        return res;
    }

    @Override
    public Result pushExport(InputTaskDetailsBo in) {
        in.setPageNo(1);
        in.setPageSize(10000);
        Pagination<TaskDetailsBo> page = pushDetailsList(in);
        List<TaskDetailsBo> list = (List<TaskDetailsBo>) page.getList();
        NewExcelUtil<TaskDetailsBo> excel=new NewExcelUtil<>(TaskDetailsBo.class);
        return excel.exportTaskDetailsExcel(list,"项目进度表",uploadPath);
    }

    @Override
    public Pagination<TaskDetailsBo> pushDetailsList(InputTaskDetailsBo in) {
        if (StringUtils.isNotBlank(in.getUpdateEndTime()))in.setUpdateEndTime(in.getUpdateEndTime()+" 23:59:59");
        // 0=查询所有,1=查询自己，2=查询下级，3=负责人查询
        if (TokenManager.hasRole(AFTConstants.SUPERADMIN)||TokenManager.hasRole(AFTConstants.APPROVAL_DECISION)){
            in.setRoleStatus(0);
        } else  {
            in.setAid(TokenManager.getAdminId());
            if (in.getRoleStatus()==null){
                in.setRoleStatus(1);
            }
        }
        Pagination<?> page = findPage("selectTaskDetailsList", "selectTaskDetailsCount", in);
        List<TaskDetails> list = (List<TaskDetails>) page.getList();
        List<TaskDetailsBo> list2=new ArrayList<>();
        for (TaskDetails e : list) {
            TaskDetailsBo bo=new TaskDetailsBo();
            BeanUtils.copyProperties(e,bo);
            addOrderInformation(bo);
            pushGetServiceProject(bo);
            list2.add(bo);
        }
        page.setList(list2);
        return (Pagination<TaskDetailsBo>) page;
    }

    private void addOrderInformation(TaskDetailsBo bo) {
        TOrderNew tOrderNew = tOrderNewMapper.selectByTid(bo.getTid());
        TOrderTask tOrderTask = tOrderTaskMapper.queryById(bo.getTid());
        TOrderMid mid = tOrderMidMapper.selectByOrderNo(tOrderNew.getOrderNo());
        //获取项目服务年份算出平均值
        List<TOrderTaskBo> tOrderTaskBos = tOrderTaskMapper.selectOrderTaskAll(tOrderNew.getOrderNo(), null);
        List<TOrderTaskBo> collect = tOrderTaskBos.stream().filter(e -> e.getSplitStatus() == 1).collect(Collectors.toList());
        List<TOrderTaskBo> collect2 = tOrderTaskBos.stream().filter(e -> e.getSplitStatus() == 0||e.getSplitStatus() == 2).collect(Collectors.toList());
        int yearSum=1;
        for (TOrderTaskBo e : collect) {
            if (e.getYearSum()!=null&&e.getYearSum()>yearSum){
                yearSum=e.getYearSum();
            }
        }
        TTaskMember tTaskMember = ttaskMemberMapper.selectByTid(tOrderTask.getId());
        String serviceYear ="";
        if (tTaskMember != null){
            serviceYear =  tTaskMember.getServiceYear();
            serviceYear = getServiceYer(serviceYear);
        }
        if (StringUtils.isEmpty(bo.getTaskName())){
            bo.setTaskName(mid.getSalesmanName());
        }
        addOrderList(collect2,serviceYear,bo);
        bo.setUserName(mid.getBuyerName());
        bo.setOrderNo(tOrderNew.getOrderNo());
        bo.setContractNo(tOrderNew.getContractNo());
        bo.setServiceYear(serviceYear);
        bo.setMemberStatus(mid.getProjectType());
        if (bo.getMemberStatus()==null){
            BigDecimal totalAmount = tOrderNew.getTotalAmount().divide(new BigDecimal(yearSum), 2, RoundingMode.HALF_UP);
            Integer lvl=getOrderLvl(totalAmount);
            bo.setMemberLvl(lvl);
        }
        bo.setFeatureList(taskFeatureMapper.selectByTdId(bo.getId()));
    }



    private String getServiceYer(String serviceYear) {
        String serviceYear2;
        if (StringUtils.isEmpty(serviceYear)){
            serviceYear2 = "";
        }else{
            if (serviceYear.contains("(赠)")){
                serviceYear2 = serviceYear.replace("(赠)", "");
            }else {
                serviceYear2 = serviceYear;
            }
        }
        return serviceYear2;
    }

    private Integer getOrderLvl(BigDecimal totalAmount) {
        Integer lvl=null;
        if (totalAmount.compareTo(new BigDecimal("3"))<0){
            lvl =1;
        }else if (totalAmount.compareTo(new BigDecimal("8"))<1){
            lvl =2;
        }else if (totalAmount.compareTo(new BigDecimal("8"))>0){
            lvl =3;
        }
        return lvl;
    }


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TaskDetails queryById(Integer id) {
        return this.taskDetailsMapper.selectById(id);
    }


    /**
     * 新增数据
     *
     * @param taskDetails 实例对象
     * @param type        0=初步新增，1=新增切同步日志
     * @return 实例对象
     */
    @Override
    public TaskDetails insert(TaskDetails taskDetails,Integer type) {
        if (type ==null)type=0;
        Date date = new Date();
        TOrderNew tOrderNew =tOrderNewMapper.selectByTid(taskDetails.getTid());
        taskDetails.setUid(tOrderNew.getBuyerId());
        taskDetails.setCreateTime(date);
        taskDetails.setUpdateTime(date);
        //数据计算
        countTaskDetails(taskDetails);
        this.taskDetailsMapper.insert(taskDetails);
        updateTask(taskDetails,tOrderNew.getOrderNo());

        //新增日志数据
        if (type==1){
            try {
                pushFeature(taskDetails);
                addTaskDetailsLog(taskDetails);
            } catch (InvocationTargetException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        return taskDetails;
    }

    private void pushFeature(TaskDetails taskDetails) {
        List<TaskFeature>  list = taskFeatureMapper.selectByTdId(taskDetails.getId());
        for (TaskFeature e : list) {
            StringBuilder str=new StringBuilder();
            String dateStr = DateUtils.formatDate(e.getStartTime(), "yyyy-MM-dd");
            str.append(dateStr).append(" 检查部门（").append(e.getDepName()).append("） ");
            String type=e.getInspectWay()==0?"线上":e.getInspectWay()==1?"线下":"无";
            str.append(type).append(";");
            if (e.getType()==1){
                taskDetails.setFeatureHt(str.toString());
            }else if (e.getType()==2){
                taskDetails.setFeatureAd(str.toString());
            }else if (e.getType()==3){
                taskDetails.setFeatureTr(str.toString());
            }else if (e.getType()==4){
                taskDetails.setFeatureTe(str.toString());
            }else if (e.getType()==5){
                taskDetails.setFeatureTi(str.toString());
            }else if (e.getType()==0){
                taskDetails.setFeatureOther(str.toString());
            }
        }
        if (StringUtils.isEmpty(taskDetails.getFeatureHt()))taskDetails.setFeatureHt("无");
        if (StringUtils.isEmpty(taskDetails.getFeatureAd()))taskDetails.setFeatureAd("无");
        if (StringUtils.isEmpty(taskDetails.getFeatureTr()))taskDetails.setFeatureTr("无");
        if (StringUtils.isEmpty(taskDetails.getFeatureTe()))taskDetails.setFeatureTe("无");
        if (StringUtils.isEmpty(taskDetails.getFeatureTi()))taskDetails.setFeatureTi("无");
        if (StringUtils.isEmpty(taskDetails.getFeatureOther()))taskDetails.setFeatureOther("无");
        taskDetailsMapper.update(taskDetails);
    }

    private void updateTask(TaskDetails taskDetails, String orderNo) {
        List<TOrderTaskBo> tOrderTaskBos = tOrderTaskMapper.selectOrderTaskAll(orderNo, null);
        List<TOrderTaskBo> collect = tOrderTaskBos.stream().filter(e -> e.getSplitStatus() == 0||e.getSplitStatus() == 2).collect(Collectors.toList());
        //如果是会员
        TOrderTaskBo thisTask = new TOrderTaskBo();
        for (TOrderTaskBo e : collect) {
            if (e.getId().equals(taskDetails.getTid())){
                thisTask=e;
                break;
            }
        }
        String serviceYear = getServiceYer(thisTask.getServiceYear());
        for (TOrderTaskBo tOrderTaskBo : collect) {
            if (StringUtils.isNotEmpty(serviceYear)){
                String serviceYer2 = getServiceYer(tOrderTaskBo.getServiceYear());
                if (serviceYear.equals(serviceYer2)){
                    TOrderTask updatedTask = new TOrderTask();
                    updatedTask.setId(tOrderTaskBo.getId());
                    updatedTask.setProgressTid(taskDetails.getId());
                    tOrderTaskMapper.update(updatedTask);
                }

            }

        }
    }

    /**
     *新增的时候特殊数据可以是单传输，修改的时候可以多传
     *
     * @param taskDetails 项目进度数据
     *
     */
    private void addTaskDetailsLog(TaskDetails taskDetails) throws InvocationTargetException, IllegalAccessException {
        Admin adminToken = TokenManager.getAdminToken();
        TaskDetailsLog log=new TaskDetailsLog();
        BeanUtils.copyProperties( taskDetails,log);
        log.setTdid(taskDetails.getId());
        log.setId(null);
        assert adminToken != null;
        log.setAid(adminToken.getId());
        log.setAname(adminToken.getName());
        log.setCreateTime(new Date());
        taskDetailsLogMapper.insert(log);
        List<TaskFeature> list = taskFeatureMapper.selectByTdId(taskDetails.getId());
        for (TaskFeature feature : list) {
            TaskFeatureLog tfLog = new TaskFeatureLog();
            BeanUtils.copyProperties(feature, tfLog);
            tfLog.setTdlId(log.getId());
            tfLog.setId(null);
            tfLog.setAid(adminToken.getId());
            tfLog.setAname(adminToken.getName());
            tfLog.setCreateTime(new Date());
            taskFeatureLogMapper.insert(tfLog);
        }

    }

    private void countTaskDetails(TaskDetails taskDetails) {
        //计算人员占比
        if (taskDetails.getWorkersNumber()!=null||
                taskDetails.getTechnologyNumber()!=null){
            if(taskDetails.getWorkersNumber()==0||taskDetails.getTechnologyNumber()==0){
                taskDetails.setWorkersRate(0.0);
            }else{
                BigDecimal wn = BigDecimal.valueOf(taskDetails.getWorkersNumber());
                BigDecimal tn = BigDecimal.valueOf(taskDetails.getTechnologyNumber());
                BigDecimal res = tn.divide(wn,4, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
                taskDetails.setWorkersRate(res.doubleValue());
            }

        }
        //计算研发预算完成进度    已归集研发费用总额÷年度研发预算总额X100%
        if (taskDetails.getActualRdAmount()!=null &&taskDetails.getRdBudget()!=null){
            if(taskDetails.getRdBudget().compareTo(BigDecimal.ZERO)==0||taskDetails.getActualRdAmount().compareTo(BigDecimal.ZERO)==0){
                taskDetails.setRdCompletionProgress(0.0);
            }else {
                BigDecimal multiply = taskDetails.getActualRdAmount().divide(taskDetails.getRdBudget(), 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
                taskDetails.setRdCompletionProgress(multiply.doubleValue());
            }
        }
        //计算年度研发占比
        //计算三年度的研发占比
            if (taskDetails.getRdBudget() != null && taskDetails.getRdSalesAmount() != null){
                BigDecimal rdBudget = taskDetails.getRdBudget();
                BigDecimal rdSalesAmount = taskDetails.getRdSalesAmount();
                if (taskDetails.getRdBudget().compareTo(BigDecimal.ZERO)==0||taskDetails.getRdSalesAmount().compareTo(BigDecimal.ZERO)==0){
                    taskDetails.setRdBudgetRate(0.0);
                }else {
                    BigDecimal res = rdBudget.divide(rdSalesAmount, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
                    taskDetails.setRdBudgetRate(res.doubleValue());
                }
                //计算三年度的研发占比
                if (taskDetails.getLastRdSalesAmount()!=null && taskDetails.getLastRdBudget()!=null&&
                        taskDetails.getBeforeLastRdSalesAmount()!=null && taskDetails.getBeforeLastRdBudget()!=null){
                    BigDecimal res2 = getBigDecimal(rdSalesAmount, rdBudget, taskDetails);
                    taskDetails.setThreeYearsRdBudgetRate(res2.doubleValue());
                }
            }
        //计算实际研发占比
        if(taskDetails.getActualSalesAmount() !=null &&
                taskDetails.getActualRdAmount() !=null){
            BigDecimal actualSalesAmount = taskDetails.getActualSalesAmount();
            BigDecimal actualRdAmount = taskDetails.getActualRdAmount();
            if (taskDetails.getActualSalesAmount().compareTo(BigDecimal.ZERO)==0||taskDetails.getActualRdAmount().compareTo(BigDecimal.ZERO)==0){
                taskDetails.setActualRdRate(0.0);
            }else {
                BigDecimal res3 = actualRdAmount.divide(actualSalesAmount, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
                taskDetails.setActualRdRate(res3.doubleValue());
            }
            if (taskDetails.getLastRdSalesAmount()!=null && taskDetails.getLastRdBudget()!=null&&
                    taskDetails.getBeforeLastRdSalesAmount()!=null && taskDetails.getBeforeLastRdBudget()!=null){
                BigDecimal res4 = getBigDecimal(actualRdAmount, actualSalesAmount, taskDetails);
                taskDetails.setThreeYearsActualRdRate(res4.doubleValue());
            }
        }
        //计算立项资料占比
        if (taskDetails.getProjectApprovalWriteNumber()!=null&& taskDetails.getProjectApprovalNumber()!=null){
            if (taskDetails.getProjectApprovalWriteNumber()==0||taskDetails.getProjectApprovalNumber()==0){
                taskDetails.setProjectApprovalWriteRate(0.0);
            }else {
                BigDecimal projectApprovalWriteNumber = BigDecimal.valueOf(taskDetails.getProjectApprovalWriteNumber());
                BigDecimal res5 = projectApprovalWriteNumber.divide(new BigDecimal(taskDetails.getProjectApprovalNumber()), 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
                taskDetails.setProjectApprovalWriteRate(res5.doubleValue());
            }
        }
        //计算项目会员等级
        TOrderNew tOrderNew = tOrderNewMapper.selectByTid(taskDetails.getTid());
        List<TOrderTaskBo> tOrderTaskBos = tOrderTaskMapper.selectOrderTaskAll(tOrderNew.getOrderNo(), 4);
        int yearSum=1;
        for (TOrderTaskBo e : tOrderTaskBos) {
            if (e.getYearSum()!=null&&e.getYearSum()>yearSum){
                yearSum=e.getYearSum();
            }
        }
        BigDecimal totalAmount = tOrderNew.getTotalAmount().divide(new BigDecimal(yearSum), 2, RoundingMode.HALF_UP);
        Integer lvl=getOrderLvl(totalAmount);
        taskDetails.setMemberLvl(lvl);
    }

    private BigDecimal getBigDecimal(BigDecimal rdSalesAmount, BigDecimal rdBudget, TaskDetails taskDetails) {
        BigDecimal threeYearsRdSalesAmount = BigDecimal.ZERO;
        BigDecimal threeYearsRdBudget = BigDecimal.ZERO;
        threeYearsRdSalesAmount = threeYearsRdSalesAmount
                .add(rdSalesAmount)
                .add(taskDetails.getLastRdSalesAmount())
                .add(taskDetails.getBeforeLastRdSalesAmount());
        threeYearsRdBudget = threeYearsRdBudget
                .add(rdBudget)
                .add(taskDetails.getLastRdBudget())
                .add(taskDetails.getBeforeLastRdBudget());
        if (threeYearsRdSalesAmount.compareTo(BigDecimal.ZERO)==0||threeYearsRdBudget.compareTo(BigDecimal.ZERO)==0){
            return BigDecimal.ZERO;
        }else {
            return threeYearsRdBudget.divide(threeYearsRdSalesAmount, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100));
        }
    }

    /**
     * 修改数据
     *
     * @param taskDetails 实例对象
     * @return 实例对象
     */
    @Override
    public TaskDetails update(TaskDetails taskDetails,Integer type) {
        taskDetails.setUpdateTime(new Date());
        if (taskDetails.getTid()==null){
            TaskDetails use = taskDetailsMapper.selectById(taskDetails.getId());
            taskDetails.setTid(use.getTid());
        }
        countTaskDetails(taskDetails);
        this.taskDetailsMapper.update(taskDetails);
        TaskDetails res = this.queryById(taskDetails.getId());
        //调用就保存，不需要区分不保存
//        if (type !=null&&type==1){
        try {
            pushFeature(taskDetails);
            addTaskDetailsLog(res);
        } catch (InvocationTargetException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
//        }
        return res;
    }

    @Override
    public Object updateDownload(TaskDetails newTd) {
        this.taskDetailsMapper.update(newTd);
        return this.queryById(newTd.getId());
    }



    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        this.taskDetailsMapper.deleteById(id);
        return true;
    }


    @Override
    public Result exportById(Integer id)  {
        NewExcelUtil<TaskDetailsBo> excel=new NewExcelUtil<>(TaskDetailsBo.class);
        TaskDetailsBo byTid = pushByTid(id);
        pushGetServiceProject(byTid);
        List<TaskDetailsBo> list = new ArrayList<>();
        list.add(byTid);
        return excel.exportTaskDetailsExcel(list,"项目进度表",uploadPath);
    }

    private void pushGetServiceProject( TaskDetailsBo res) {
        List<Map<String, Object>> taskList = res.getTaskList();
        String serviceYear = null;
        if (taskList!=null){
            StringBuilder sb = new StringBuilder();
            for (Map<String, Object> map : taskList) {
                sb.append(map.get("taskName")).append("-").append(map.get("serviceYear")).append(",");
                if (serviceYear==null)serviceYear = (String) map.get("serviceYear");
            }
            res.setServiceYear(serviceYear);
            res.setServiceProject(sb.substring(0,sb.length()-1));
        }
    }

    @Override
    public boolean checkTid(TaskDetails taskDetails) {
        TaskDetails res = taskDetailsMapper.selectByTid(taskDetails.getTid());
        return res != null;
    }

    @Override
    public TaskFeature addFeature(InputTaskFeature in) {
        in.setCreateTime(new Date());
        if (in.getStartTimeStr()!=null)in.setStartTime(DateUtils.parseDate(in.getStartTimeStr()));
        if (in.getEndTimeStr()!=null)in.setEndTime(DateUtils.parseDate(in.getEndTimeStr()));
        taskFeatureMapper.insert(in);
        return in;
    }

    @Override
    public TaskFeature updateFeature(InputTaskFeature in) {
        if (in.getStartTimeStr()!=null)in.setStartTime(DateUtils.parseDate(in.getStartTimeStr()));
        if (in.getEndTimeStr()!=null)in.setEndTime(DateUtils.parseDate(in.getEndTimeStr()));
        taskFeatureMapper.update(in);
        return taskFeatureMapper.selectById(in.getId());
    }

    @Override
    public Object logList(Integer id, Integer pageNo, Integer pageSize) {
        Map<String,Object> param= new HashMap<>();
        param.put("tdid",id);
        if (pageNo ==null)pageNo=1;
        if (pageSize ==null)pageSize=10;
        return findPage("selectByTidList", "selectByTidCount", param, pageNo, pageSize);
    }

    @Override
    public Object logDetails(Integer id) {
        TaskDetailsLog log = taskDetailsLogMapper.selectById(id);
        TaskDetails taskDetails = taskDetailsMapper.selectById(log.getTdid());
        Integer tid = taskDetails.getTid();
        TaskDetailsBo res = new TaskDetailsBo();
        TOrderNew tOrderNew = tOrderNewMapper.selectByTid(tid);
        TOrderMid mid = tOrderMidMapper.selectByOrderNo(tOrderNew.getOrderNo());
        List<TOrderTaskBo> tOrderTaskBos = tOrderTaskMapper.selectOrderTaskAll(tOrderNew.getOrderNo(), 4);
        //如果是会员
        TOrderTaskBo thisTask = new TOrderTaskBo();
        for (TOrderTaskBo e : tOrderTaskBos) {
            if (e.getId().equals(tid)){
                thisTask=e;
                break;
            }
        }
        String serviceYear = getServiceYer(thisTask.getServiceYear());
        addOrderList(tOrderTaskBos, serviceYear,res);
        if (taskDetails !=null){
                BeanUtils.copyProperties(log, res);
        }
        res.setUserName(mid.getBuyerName());
        res.setOrderNo(tOrderNew.getOrderNo());
        res.setContractNo(tOrderNew.getContractNo());
        res.setMemberStatus(mid.getProjectType());
        BigDecimal totalAmount = tOrderNew.getTotalAmount();
        if (res.getMemberLvl()==null){
            Integer lvl=getOrderLvl(totalAmount);
            res.setMemberLvl(lvl);
        }
        res.setFeatureList(taskFeatureLogMapper.selectByTdlid(id));
        return res;
    }

    private void addOrderList(List<TOrderTaskBo> tOrderTaskBos, String serviceYear,TaskDetailsBo bo) {
        List<Map<String, Object>> list =new ArrayList<>();
        //判定是否是需要修改,只有在没有财税和技术，并且编号不是空才需要新增
        boolean flag=false;
        for (TOrderTaskBo e : tOrderTaskBos) {
            String serviceYear1 = getServiceYer(e.getServiceYear());
            if (serviceYear1.equals(serviceYear)){
                Map<String,Object> map=new HashMap<>();
                map.put("serviceYear", e.getServiceYear());
                map.put("taskName", e.getCommodityName());
                map.put("id",e.getId());
                list.add(map);
                if (StringUtils.isEmpty(bo.getFinanceTaxationName())){
                    if (e.getCommodityName().contains("财税")){
                        bo.setFinanceTaxationName(e.getReceiverName());
                    }
                    if (bo.getId()!=null) flag=true;
                }
                if (StringUtils.isEmpty(bo.getTechnologyName())){
                    if (e.getCommodityName().contains("技术")){
                        bo.setTechnologyName(e.getReceiverName());
                    }
                    if (bo.getId()!=null) flag=true;
                }
            }
            if (e.getId().equals(bo.getTid())
                    &&StringUtils.isEmpty(bo.getProjectName())){
                bo.setProjectName(e.getReceiverName());
            }
        }
        if (flag){
            TaskDetails update = new TaskDetails();
            update.setId(bo.getId());
            update.setFinanceTaxationName(bo.getFinanceTaxationName());
            update.setTechnologyName(bo.getTechnologyName());
            if (update.getFinanceTaxationName()!=null||
                update.getTechnologyName()!=null){
                taskDetailsMapper.update(update);
            }

        }
        bo.setTaskList(list);
    }

    @Override
    public Object deleteTaskFeature(Integer id) {
        return taskFeatureMapper.deleteById(id);
    }




}
