package io.renren.modules.business.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.business.dao.BusinessContractDao;
import io.renren.modules.business.dao.BusinessProjectDao;
import io.renren.modules.business.dao.BusinessTotalDao;
import io.renren.modules.business.entity.BusinessContractEntity;
import io.renren.modules.business.entity.BusinessTotalEntity;
import io.renren.modules.business.service.BusinessTotalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;


@Service("businessTotalService")
public class BusinessTotalServiceImpl extends ServiceImpl<BusinessTotalDao, BusinessTotalEntity> implements BusinessTotalService {

    @Autowired
    private BusinessProjectDao businessProjectDao;

    @Autowired
    private BusinessContractDao businessContractDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BusinessTotalEntity> page = new Query<BusinessTotalEntity>().getPage(params);
        List<BusinessTotalEntity> list = baseMapper.selectTotalAll(params);
        page.setRecords(list);
        return new PageUtils(page);
    }

    /**
     * 根据总结时间获取所有的立项信息
     * @param time
     * @return
     */
    @Override
    public List<BusinessTotalEntity> getListByTime(String time) {
        return baseMapper.getListByTime(time);
    }

    /**
     * 今日总结总结时间List
     * @return
     */
    @Override
    public List<String> getTimeList() {
        return baseMapper.getTimeList();
    }

    /**
     * 根据立项id获取所有的总结列表
     */
    @Override
    public List<BusinessTotalEntity> getListByProjectId(String projectId) {
        return baseMapper.getListByProjectId(projectId);
    }

    /**
     * 内容复制通过时间 获取符合条件的list 包括立项 合同 复制信息
     * @param time
     * @return
     */
    @Override
    public Map<String, Object> getListByTimeNew(String time) {
        //所有信息整合的map数组
        Map<String, Object> mapAll = new HashMap<>();
        //根据时间获取的对应的所有的计划的list
        List<BusinessTotalEntity> list = baseMapper.getListByTime(time);
        //ids 字符串查询合同用
        String ids = "";
        //copy字符串返回到前端为了    使用一键复制功能
        String copy = "今日总结";
        copy +="\n\n" + "时间:" + time;
        //遍历 获取立项ids
        if (list.size() > 0) {
            for (BusinessTotalEntity i : list) {
                if (ids.equals("")) {
                    ids = i.getTotalProjectId().toString();
                } else {
                    ids += "," + i.getTotalProjectId().toString();
                }
            }
        }
        //根据ids 获取的合同信息
        List<Map<String, Object>> contractList = new ArrayList<Map<String, Object>>();
        //合同map   数据形式：立项id+合同所有的信息
        Map<String, Object> map = new HashMap<>();
        if (list.size() > 0) {
            //遍历ids获取对应的合同信息
            for (String id : ids.split(",")) {
                List<BusinessContractEntity> businessContractEntitys = businessContractDao.getByProjectId(id);
                if(businessContractEntitys.size() > 0){
                    map.put(id, businessContractEntitys.get(0));
                }
            }
            contractList.add(map);
        }
        //将立项信息 合同信息  数据拼入到复制所用的字符串中
        if (list.size() > 0) {//
            for (BusinessTotalEntity i : list) {
//                copy +="\n\n" +  "立项名称：" + (i.getProjectNewName() != null ? i.getProjectNewName() : i.getProject()) + "\n"
//                        + "1.立项属性\n" + "\t①立项名称:" + (i.getProjectNewName() != null ? i.getProjectNewName() : i.getProject()) + "\n"
//                        + "\t②建设单位：" + (i.getJsdw() != null?i.getJsdw():"暂无") + "\n" + "\t③施工地点：" + (i.getSgdd() != null?i.getSgdd():"暂无") + "\t④年份：" + (i.getProjectYear() != null?i.getProjectYear():"暂无") + "\n" + "\t⑤开工时间：" + (i.getProjectStart()!= null?dateToString(i.getProjectStart()):"暂无") + "\n"
//                        + "\t⑥竣工时间：" + (i.getProjectEnd() != null?dateToString(i.getProjectEnd()):"暂无") + "\n" + "\t⑦施工进度：" + (i.getSgjd() != null?i.getSgjd():"暂无") + "\n\t⑧负责工长：" + (i.getFzgz() != null?i.getFzgz():"暂无")+"\n\t⑨联络员："+(i.getLly() != null?i.getLly():"暂无")+"\n\t⑩工程类别："+(i.getGclb() != null?i.getGclb():"暂无") +"\n\r";
//                if (map.get(i.getTotalProjectId().toString()) != null) {
//                    BusinessContractEntity busCon = (BusinessContractEntity) map.get(i.getTotalProjectId().toString());
//                    //System.out.println(busCon);
//                    copy +=  "2.合同属性：" + "\n\t" + "①合同名称：" + (busCon.getContractNewName() != null ? busCon.getContractNewName() : busCon.getContractName())+ "\n" + "\t②合同年份：" + (busCon.getContractYear() != null?busCon.getContractYear():"暂无") + "\n"
//                            + "\t③直非友：" + (busCon.getZfy() != null?busCon.getZfy():"暂无")  + "\n" + "\t④施工单位：" + (busCon.getSgdw() != null?busCon.getSgdw():"暂无") + "\n"
//                            + "\t⑤监理单位：" + (busCon.getJldw() != null?busCon.getJldw():"暂无") + "\n"  + "\t⑥一审单位：" + (busCon.getYsdw() != null?busCon.getYsdw():"暂无") + "\n" + "\t⑦二审单位：" + (busCon.getEsdw() != null?busCon.getEsdw():"暂无") + "\n\r";
//                } else {
//                    copy += "\t暂无合同属性" + "\n\n";
//                }
//                copy += "\r" + "3.是否一天：" + i.getOneDay() + "\n" + "4.施工人员：" + (i.getSelectPer() != null?i.getSelectPer():"暂无") + "\n"
//                        + "5.施工内容：" +(i.getTotalSgSummary() != null?i.getTotalSgSummary():"暂无") + "\n" + "6.材料：" +(i.getTotalCl() != null?i.getTotalCl():"暂无") + "\n"
//                        + "7.机械：" +(i.getTotalJx() != null?i.getTotalJx():"暂无") + "\n";

//                copy +="\n\n" +  "立项名称：" + (i.getProjectNewName() != null ? i.getProjectNewName() : i.getProject()) + "\n";
//                copy += "\r" + "1.是否一天：" + i.getOneDay() + "\n" + "2.施工人员：" + (i.getSelectPer() != null?i.getSelectPer():"暂无") + "\n"
//                        + "3.施工内容：" +(i.getTotalSgSummary() != null?i.getTotalSgSummary():"暂无") + "\n" + "4.材料：" +(i.getTotalCl() != null?i.getTotalCl():"暂无") + "\n"
//                        + "5.机械：" +(i.getTotalJx() != null?i.getTotalJx():"暂无") + "\n";
                copy +="\n\n" +  "立项名称：" + (i.getProjectNewName() != null ? i.getProjectNewName() : i.getProject()) + "\n";
                copy += "\r" + "①.是否一天：" + i.getOneDay() + "\n" + "②.施工人员：" + (i.getSelectPer() != null?i.getSelectPer():"暂无") + "\n"
                        + "③.完成内容：" +(i.getTotalSgSummary() != null?i.getTotalSgSummary():"暂无") + "\n" + "④.材料、机械及工具：" +(i.getTotalCl() != null?i.getTotalCl():"暂无") + "\n"
                        + "⑤.自检总结：" +(i.getTotalJx() != null?i.getTotalJx():"暂无") + "\n";

            }
        } else {
//            copy += "\t\t暂无立项信息\n暂无合同信息\n\n";
            copy += "\t\t暂无立项信息\n";
        }
        mapAll.put("list", list);
        mapAll.put("contractList", contractList);
        mapAll.put("copy", copy);
        return mapAll;
    }

    /**
     * 详情包含关联信息
     * @param totalId
     * @return
     */
    @Override
    public BusinessTotalEntity getByIdNew(Integer totalId) {
        return baseMapper.getByIdNew(totalId);
    }

    @Override
    public List<BusinessTotalEntity> totalList(Map<String, Object> params) {
        List<BusinessTotalEntity> list = baseMapper.totalList(params);
        return list;
    }

    /**
     * 格式化时间
     */
    private String dateToString(Date d){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(d);
    }

    @Override
    public List<BusinessTotalEntity> getUncommittedList() {
        return baseMapper.getUncommittedList();
    }

}