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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.BusinessPlanDao;
import io.renren.modules.business.dao.BusinessProjectDao;
import io.renren.modules.business.dao.BusinessWorkerDao;
import io.renren.modules.business.entity.BusinessPlanEntity;
import io.renren.modules.business.entity.BusinessProjectEntity;
import io.renren.modules.business.entity.BusinessWorkerEntity;
import io.renren.modules.business.service.BusinessPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


@Service("businessPlanService")
public class BusinessPlanServiceImpl extends ServiceImpl<BusinessPlanDao, BusinessPlanEntity> implements BusinessPlanService {

    @Autowired
    private BusinessProjectDao businessProjectDao;

    @Autowired
    private BusinessContractDao businessContractDao;

    @Autowired
    private BusinessWorkerDao businessWorkerDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper qw = new QueryWrapper<BusinessPlanEntity>();
        if (params.get("planProject") != null && !params.get("planProject").equals("") && !params.get("planProject").equals("undefined")) {
            qw.like("plan_project", params.get("planProject"));
        }
        IPage<BusinessPlanEntity> page = this.page(
                new Query<BusinessPlanEntity>().getPage(params),
                qw
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryNewPage(Map<String, Object> params) {
        QueryWrapper qw = new QueryWrapper<BusinessPlanEntity>();
        qw.orderByDesc("revise");
        if (params.get("planProject") != null && params.get("planProject") != "" && params.get("planProject") != "undefined") {
            qw.like("plan_project", params.get("planProject"));
        }
        IPage<BusinessPlanEntity> page = this.page(
                new Query<BusinessPlanEntity>().getPage(params),
                qw
        );
        return new PageUtils(page);
    }

    @Override
    public List<BusinessPlanEntity> queryListPage(Map<String, Object> params) {
        return null;
    }

    /**
     * 获取所有计划信息带表关联  排序
     *
     * @return
     */
    @Override
    public PageUtils selectAll(Map<String, Object> params) {
        IPage<BusinessPlanEntity> page = new Query<BusinessPlanEntity>().getPage(params);
        List<BusinessPlanEntity> list = baseMapper.selectAll(params);
        page.setRecords(list);
        return new PageUtils(page);
    }

    /**
     * 计划综述通过时间 获取符合条件的list
     */
    @Override
    public List<BusinessPlanEntity> getListByTime(String time) {
        return baseMapper.getListByTime(time);
    }

    /**
     * 获取计划中的已经提交的信息
     */
    @Override
    public PageUtils getWZjList(Map<String, Object> params) {
        IPage<BusinessPlanEntity> page = new Query<BusinessPlanEntity>().getPage(params);
        List<BusinessPlanEntity> list = baseMapper.getWZjList(params);
        page.setRecords(list);
        return new PageUtils(page);
    }

    /**
     * 获取已有计划的计划时间（未删除，未提交）
     *
     * @return
     */
    @Override
    public List<BusinessPlanEntity> getTimeList() {
        return baseMapper.getTimeList();
    }

    /**
     * 根据立项id  获取立项 合同  明日计划信息
     *
     * @param projectId
     * @return
     */
    @Override
    public Map<String, Object> lxHtPlanInfo(String projectId) {
        Map<String, Object> mapList = new HashMap<>();
        //ids获取的合同信息
        String[] ids = projectId.split(",");
        Map<String, Object> map = new HashMap<>();
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        for (String id : ids) {
            map.put(id, businessContractDao.getByProjectId(id));
        }
        list.add(map);
        //ids 获取的立项信息
        List<BusinessProjectEntity> lxList = businessProjectDao.queryPerojectList(ids);
        //根据立项id获取最新的明日计划信息
        BusinessPlanEntity businessPlanEntity = baseMapper.getNewInfo(projectId);
        mapList.put("contract", map);
        mapList.put("project", lxList);
        mapList.put("plan", businessPlanEntity);
        return mapList;
    }

    /**
     * 获取首页滚动信息
     *
     * @return
     */
    @Override
    public List<BusinessPlanEntity> getGdList() {
        //获取当前系统时间
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String nowTime = sdf.format(c.getTime());
        System.out.println(nowTime);
        return baseMapper.getGdList(nowTime);
    }

    /**
     * 根据时间获取的  所有立项 合同  复制 信息
     *
     * @param time
     * @return
     */
    @Override
    public Map<String, Object> getListByTimeNew(String time) {
        //所有信息整合的map数组
        Map<String, Object> mapAll = new HashMap<>();
        //根据时间获取的对应的所有的计划的list
        List<BusinessPlanEntity> list = baseMapper.getListByTime(time);
        //ids 字符串查询合同用
        String ids = "";
        //copy字符串返回到前端为了    使用一键复制功能
        String copy = "明日计划";
        copy +="\n\n" + "时间:" + time;
        //遍历 获取立项ids
        if (list.size() > 0) {
            for (BusinessPlanEntity i : list) {
                if (ids.equals("")) {
                    ids = i.getPlanProjectId().toString();
                } else {
                    ids += "," + i.getPlanProjectId().toString();
                }
            }
        }
        //根据ids获取所有的立项信息
        List<BusinessProjectEntity> projectList = businessProjectDao.queryPerojectList(ids.split(","));
        //根据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(",")) {
                if(businessContractDao.getByProjectId(id).size() > 0){
                    map.put(id, businessContractDao.getByProjectId(id).get(0));
                }
            }
            contractList.add(map);
        }
        //将立项信息 合同信息  数据拼入到复制所用的字符串中
        if (projectList.size() > 0) {//
            for (BusinessPlanEntity j : list) {  //  计划信息
//                for (BusinessProjectEntity i : projectList) {  // 里面的立项信息
//                    if (j.getPlanProjectId().equals(i.getProjectId())) { // 判断当前立项是不是选中的立项
//                        System.out.println(j);
//                        System.out.println(i);
//                        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():"暂无") + "\n"+ "\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.getProjectId().toString()) != null) { // 如果合同信息不为空显示合同信息
//                            BusinessContractEntity busCon = (BusinessContractEntity) map.get(i.getProjectId().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.是否一天：" + j.getOneDay() + "\n" + "4.施工人员：" + (j.getSelectPer() != null?j.getSelectPer():"暂无") + "\n"
//                        + "5.施工内容：" +(j.getPlanSgSummary() != null?j.getPlanSgSummary():"暂无") + "\n" + "6.材料：" +(j.getPlanCl() != null?j.getPlanCl():"暂无") + "\n"
//                        + "7.机械：" +(j.getPlanJx() != null?j.getPlanJx():"暂无") + "\n";
                for (BusinessProjectEntity i : projectList) {  // 里面的立项信息
                    if (j.getPlanProjectId().equals(i.getProjectId())) { // 判断当前立项是不是选中的立项
                        copy +="\n\n" +  "立项名称：" + (i.getProjectNewName() != null ? i.getProjectNewName() : i.getProject()) + "\n";
                    }
                }
//                copy += "\r" + "1.是否一天：" + j.getOneDay() + "\n" + "2.施工人员：" + (j.getSelectPer() != null?j.getSelectPer():"暂无") + "\n"
//                        + "3.施工内容：" +(j.getPlanSgSummary() != null?j.getPlanSgSummary():"暂无") + "\n" + "4.材料：" +(j.getPlanCl() != null?j.getPlanCl():"暂无") + "\n"
//                        + "5.机械：" +(j.getPlanJx() != null?j.getPlanJx():"暂无") + "\n";
                copy += "\r" + "①.是否一天：" + j.getOneDay() + "\n" + "②.施工人员：" + (j.getSelectPer() != null?j.getSelectPer():"暂无") + "\n"
                        + "③.预计完成内容：" +(j.getPlanSgSummary() != null?j.getPlanSgSummary():"暂无") + "\n" + "④.材料、机械及工具：" +(j.getPlanCl() != null?j.getPlanCl():"暂无") + "\n"
                        + "⑤.自检总结：" +(j.getPlanJx() != null?j.getPlanJx():"暂无") + "\n";
            }
        } else {
//            copy += "\t\t暂无立项信息\n暂无合同信息\n\n";
            copy += "\t\t暂无立项信息\n";
        }
        mapAll.put("list", list);
        mapAll.put("projectList", projectList);
        mapAll.put("contractList", contractList);
        mapAll.put("copy", copy);
        return mapAll;
    }

    /**
     * 详情信息   包括所有的关联的信息
     * @param planId
     * @return
     */
    @Override
    public BusinessPlanEntity getInfoNew(Integer planId) {
        return baseMapper.getInfoNew(planId);
    }

    /**
     * 根据传入的计划时间对人员进行处理
     * @param time
     * @return
     */
    @Override
    public List<BusinessWorkerEntity> getNewRyList(String time) {
        // 获取素有的可分配未离职的人员列表
        List<BusinessWorkerEntity> workerList = businessWorkerDao.queryfpWlzList();
        // 根据选择的时间获取处理后的带队人id  字段和选择人的字段
        BusinessPlanEntity bEntity = baseMapper.getSJList(time);
        if(bEntity != null){
            // 所有带队人的idsList没有去重
            String[] ddIds = bEntity.getDd().split(",");
            // 所有施工人员的idsList没有去重
            String[] selectIds = bEntity.getSelectPerIds().split(",");
            // 过滤后的所有的无重复的id数组 默认的数据长度是两个字段都没有重复的id两个数组长度之和
            ArrayList<String> ids = new ArrayList<>();
            // 带队人加入到综合数组中
            for (String id : ddIds){
                // 包含标志
                int flag = 0;
                // 遍历判断包不包含
                if(ids.contains(id)){
                    flag = 1;
                }
                // 不包含加入数组中
                if (flag == 0) {
                    ids.add(id);
                }
            }
            // 施工人员人加入到综合数组中
            for (String id : selectIds){
                // 包含标志
                int flag = 0;
                // 遍历判断包不包含
                if(ids.contains(id)){
                    flag = 1;
                }
                // 不包含加入数组中
                if (flag == 0) {
                    ids.add(id);
                }
            }
            for(int i = 0;i < workerList.size();i++){
                // 说明这个人在选择的时间中被选择了需要去除
                if(ids.contains(workerList.get(i).getUserId().toString())){
                    workerList.remove(i);
                }
            }
        }
        return workerList;
    }

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

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

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

}