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.DateUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.business.dao.BusinessDictDao;
import io.renren.modules.business.dao.BusinessWorkerDao;
import io.renren.modules.business.dao.BusinessWorkerOrgDao;
import io.renren.modules.business.entity.BusinessPlanEntity;
import io.renren.modules.business.entity.BusinessWorkerEntity;
import io.renren.modules.business.entity.BusinessWorkerOrgEntity;
import io.renren.modules.business.service.BusinessPlanService;
import io.renren.modules.business.service.BusinessWorkerOrgService;
import io.renren.modules.business.service.BusinessWorkerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("businessWorkerService")
public class BusinessWorkerServiceImpl extends ServiceImpl<BusinessWorkerDao, BusinessWorkerEntity> implements BusinessWorkerService {

    @Autowired
    BusinessDictDao businessDictDao;

    @Autowired
    BusinessWorkerOrgDao businessWorkerOrgDao;

    @Autowired
    BusinessWorkerOrgService businessWorkerOrgService;

    @Autowired
    BusinessPlanService businessPlanService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        /*QueryWrapper<BusinessWorkerEntity> qw = new QueryWrapper<>();
        if (params.get("workId") != null && !params.get("workId").equals("") && !params.get("workId").equals("undefined")) {
            qw.eq("work_id", params.get("workId"));
        }
        if (params.get("positionId") != null && !params.get("positionId").equals("") && !params.get("positionId").equals("undefined")) {
            qw.eq("position_id", params.get("positionId"));
        }
        if (params.get("status") != null && !params.get("status").equals("") && !params.get("status").equals("undefined")) {
            qw.eq("status", params.get("status"));
        }
         if (params.get("isSafe") != null && !params.get("isSafe").equals("") && !params.get("isSafe").equals("undefined")) {
            qw.eq("is_safe", params.get("isSafe"));
        }
        if (params.get("jobId") != null && !params.get("jobId").equals("") && !params.get("jobId").equals("undefined")) {
            qw.eq("job_id", params.get("jobId"));
        }
        if (params.get("gjz") != null && !params.get("gjz").equals("") && !params.get("gjz").equals("undefined")) {
            //先判断是不是字典里面包含的
            int id = 0;//记录字典查询出来的id
            int zwzt = -1;
            List<BusinessDictEntity> dic = businessDictDao.queryWzAndZw();
            for (BusinessDictEntity gjz : dic) {
                if (gjz.getName().equals(params.get("gjz"))) {
                    id = gjz.getDictId();
                }
            }
            //在职状态
            if (params.get("gjz").toString().contains("在职")) {
                zwzt = 0;
            } else if (params.get("gjz").toString().contains("离职")) {
                zwzt = 1;
            }
            if (id != 0 && zwzt != -1) {
                int ids = id;
                int zwzts = zwzt;
                Consumer<QueryWrapper<BusinessWorkerEntity>> consumer = qw2 -> {
                    qw2.like("name", params.get("gjz"));
                    qw2.or();
                    qw2.like("age", params.get("gjz"));
                    qw2.or().eq("position_id", ids).or().eq("work_id", ids).or().eq("job_id", ids).or().eq("status", zwzts);
                };
                qw.and(consumer);
            } else if (id != 0) {
                int ids = id;
                Consumer<QueryWrapper<BusinessWorkerEntity>> consumer = qw2 -> {
                    qw2.like("name", params.get("gjz"));
                    qw2.or();
                    qw2.like("age", params.get("gjz"));
                    qw2.or().eq("position_id", ids).or().eq("work_id", ids);
                };
                qw.and(consumer);
            } else if (zwzt != -1) {
                int zwzts = zwzt;
                Consumer<QueryWrapper<BusinessWorkerEntity>> consumer = qw2 -> {
                    qw2.like("name", params.get("gjz"));
                    qw2.or();
                    qw2.like("age", params.get("gjz"));
                    qw2.or().eq("status", zwzts);
                };
                qw.and(consumer);
                //System.out.println(qw);
            } else {
                Consumer<QueryWrapper<BusinessWorkerEntity>> consumer = qw2 -> {
                    qw2.like("name", params.get("gjz"));
                    qw2.or();
                    qw2.like("age", params.get("gjz"));
                };
                qw.and(consumer);
            }
        }
        qw.orderByAsc("order_num");
        qw.orderByDesc("user_id");
        IPage<BusinessWorkerEntity> page = this.page(
                new Query<BusinessWorkerEntity>().getPage(params),
                qw
        );*/
        //System.out.println(params);

        IPage<BusinessWorkerEntity> page = new Query<BusinessWorkerEntity>().getPage(params);
        List<BusinessWorkerEntity> list = baseMapper.selectAllContainsDeparture(params);
        page.setRecords(list);

        return new PageUtils(page);
    }

    @Override
    public List<BusinessWorkerEntity> queryNewPage(Map<String, Object> params) {
        return baseMapper.queryNewPage();
    }

    //包含离职列表
    @Override
    public PageUtils selectAllContainsDeparturePage(Map<String, Object> params) {
        IPage<BusinessWorkerEntity> page = new Query<BusinessWorkerEntity>().getPage(params);
        List<BusinessWorkerEntity> list = baseMapper.selectAllContainsDeparture(params);
        page.setRecords(list);
        return new PageUtils(page);
    }

    //包含离职列表
    @Override
    public List<BusinessWorkerEntity> selectAllContainsDeparture(Map<String, Object> params) {
        return baseMapper.selectAllContainsDeparture(params);
    }

    /**
     * 可分配未离职人员名单
     * @return
     */
    @Override
    public List<BusinessWorkerEntity> queryfpWlzList() {
        return baseMapper.queryfpWlzList();
    }

    /**
     * 获取人员职责信息
     *
     * @return
     */
    @Override
    public PageUtils orgDutyList(Map<String, Object> params){
        IPage<Map<String,Object>> page = new Query<Map<String,Object>>().getPage(params);
        List<Map<String,Object>> list = baseMapper.orgDutyList();
        page.setRecords(list);
        return new PageUtils(page);
    }

    /**
     * 机构框图信息
     *
     * @return
     */
    @Override
    public BusinessWorkerOrgEntity jgktList() {
        return businessWorkerOrgDao.jgktList();
    }

    /**
     * 职位操作
     *
     * @param params
     */
    @Override
    public void savdutyOperatee(Map<String, Object> params) {
        System.out.println(params);
        //根据职责id更新职责信息
        if (params.get("zzId") != null&&!params.get("zzId").equals("")) {
            BusinessWorkerOrgEntity businessWorkerOrgEntity = new BusinessWorkerOrgEntity();
            businessWorkerOrgEntity.setId(Integer.parseInt(params.get("zzId").toString()));
            businessWorkerOrgEntity.setOrgDuty((String) params.get("orgDuty"));
            businessWorkerOrgService.updateById(businessWorkerOrgEntity);
        }else{
            BusinessWorkerOrgEntity businessWorkerOrgEntity = new BusinessWorkerOrgEntity();
            businessWorkerOrgEntity.setOrgWorker(Integer.parseInt(params.get("workId").toString()));
            businessWorkerOrgEntity.setOrgPosition(Integer.parseInt(params.get("positionId").toString()));
            businessWorkerOrgEntity.setOrgDuty((String) params.get("orgDuty"));
            businessWorkerOrgEntity.setType("1");
            businessWorkerOrgService.save(businessWorkerOrgEntity);
        }
        if (params.get("userId") != null) {
            BusinessWorkerEntity old = new BusinessWorkerEntity();//原始位置姓名数据
            old.setUserId(Integer.parseInt(params.get("userId").toString()));
            old.setWorkId(null);
            old.setPositionId(null);
            BusinessWorkerEntity newWorker = new BusinessWorkerEntity();//新姓名数据
            newWorker.setUserId(Integer.parseInt(params.get("newId").toString()));
            newWorker.setWorkId(Integer.parseInt(params.get("workId").toString()));
            newWorker.setPositionId(Integer.parseInt(params.get("positionId").toString()));
            //更新  位置id 和 职位id
            baseMapper.updateWPById(old);
            baseMapper.updateWPById(newWorker);
        }
    }

    /**
     * 获取所有录入的员工信息
     * @param params
     * @return
     */
    @Override
    public List<BusinessWorkerEntity> allWorkerList() {
        return baseMapper.allWorkerList();
    }

    @Override
    public Integer findCnByName(Map<String, Object> params){
        return baseMapper.findCnByName(params);
    }

    /**
     * 查询全部带队，明日计划
     *
     * @param businessPlanEntity
     * @return
     */
    @Override
    public List<BusinessWorkerEntity> queryDdry4Plan(BusinessPlanEntity businessPlanEntity) {
        List<BusinessWorkerEntity> result = new ArrayList<>();
        // 全部施工人员
        List<BusinessWorkerEntity> workerList = baseMapper.queryDdry();
        String time = DateUtils.format(businessPlanEntity.getPlanTime());
        // 按时间查询计划
        List<BusinessPlanEntity> planList = businessPlanService.getListByTime(time);
        if(planList.size() > 0) {
            // 如果是一天的任务，则取查询不在计划中的
            // 如果是半天，只取不在计划中和只有半天被分配出去的
            for(BusinessWorkerEntity worker : workerList) {
                boolean has = false;
                // 半天的次数
                int bantianCnt = 0;
                for(BusinessPlanEntity plan : planList) {
                    if(plan.getPlanDdId() != null && (","+plan.getPlanDdId()+",").indexOf(","+worker.getUserId()+",") != -1) {
                        has = true;
                        if("否".equals(plan.getOneDay())) {
                            bantianCnt++;
                        }
                    }
                }
                if(!has) {
                    result.add(worker);
                } else if("否".equals(businessPlanEntity.getOneDay()) && bantianCnt == 1) {
                    worker.setName(worker.getName()+"*");
                    result.add(worker);
                }
            }

        } else {
            result = workerList;
        }
        return result;
    }

    /**
     * 查询全部施工人员，明日计划
     *
     * @return
     */
    @Override
    public List<BusinessWorkerEntity> querySgry4Plan(Map<String, Object> params) {
//        List<BusinessWorkerEntity> result = new ArrayList<>();
//        // 全部施工人员
//        List<BusinessWorkerEntity> workerList = baseMapper.querySgry();
//        String time = DateUtils.format(businessPlanEntity.getPlanTime());
//        // 按时间查询计划
//        List<BusinessPlanEntity> planList = businessPlanService.getListByTime(time);
//        if(planList.size() > 0) {
//            // 如果是一天的任务，则取查询不在计划中的
//            // 如果是半天，只取不在计划中和只有半天被分配出去的
//            for(BusinessWorkerEntity worker : workerList) {
//                boolean has = false;
//                // 半天的次数
//                int bantianCnt = 0;
//                for(BusinessPlanEntity plan : planList) {
//                    if(plan.getSelectPersionId() != null && (","+plan.getSelectPersionId()+",").indexOf(","+worker.getUserId()+",") != -1) {
//                        has = true;
//                        if("否".equals(plan.getOneDay())) {
//                            bantianCnt++;
//                        }
//                    }
//                }
//                if(!has) {
//                    result.add(worker);
//                } else if("否".equals(businessPlanEntity.getOneDay()) && bantianCnt == 1) {
//                    worker.setName(worker.getName()+"*");
//                    result.add(worker);
//                }
//            }
//
//        } else {
//            result = workerList;
//        }

        List<BusinessWorkerEntity> workerList = baseMapper.querySgry(params);
        return workerList;
    }

    @Override
    public List<BusinessWorkerEntity> selectName() {
        List<BusinessWorkerEntity> businessWorkerEntityList = baseMapper.selectName();
        return businessWorkerEntityList;
    }


}
