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.BusinessDictDao;
import io.renren.modules.business.dao.BusinessProjectDao;
import io.renren.modules.business.entity.BusinessProjectEntity;
import io.renren.modules.business.service.BusinessProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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


@Service("businessProjectService")
public class BusinessProjectServiceImpl extends ServiceImpl<BusinessProjectDao, BusinessProjectEntity> implements BusinessProjectService {

    @Autowired
    BusinessDictDao businessDictDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        /*QueryWrapper<BusinessProjectEntity> qw = new QueryWrapper<>();
        qw.eq("status",0);
        //负责工长id
        if(params.get("projectFzgzId") != null && !params.get("projectFzgzId").equals("") && !params.get("projectFzgzId").equals("undefined")){
            qw.eq("project_fzgz_id",params.get("projectFzgzId"));
        }
        //健设单位id（中包含其他在前端做的传值）
        if (params.get("projectJsdwId") != null && !params.get("projectJsdwId").equals("") && !params.get("projectJsdwId").equals("undefined")) {
            qw.eq("project_jsdw_id", params.get("projectJsdwId"));
        }
        //年份
        if (params.get("projectYear") != null && !params.get("projectYear").equals("") && !params.get("projectYear").equals("undefined")) {
            qw.eq("project_year", params.get("projectYear"));
        }
        //工程类别
        if (params.get("projectGclbId") != null && !params.get("projectGclbId").equals("") && !params.get("projectGclbId").equals("undefined")) {
            String strSql = null;
            if(params.get("projectGclbId").toString().contains(",")){
                String[] str = params.get("projectGclbId").toString().split(",");
                for(int i = 0;i < str.length;i++){
                    if(strSql == null){
                        strSql = " and concat(',',project_gclb_id,',') like '%,"+ str[i] +",%' ";
                    } else {
                        strSql += " and concat(',',project_gclb_id,',') like '%,"+ str[i] +",%' ";
                    }
                }
            }else{
                strSql = " and concat(',',project_gclb_id,',') like '%,"+ params.get("projectGclbId").toString() +",%' ";
            }
            qw.last(strSql);
        }
        //施工进度
        if (params.get("projectSgjdId") != null && !params.get("projectSgjdId").equals("") && !params.get("projectSgjdId").equals("undefined")) {
            qw.eq("project_sgjd_id", params.get("projectSgjdId"));
        }
        //施工地点
        if (params.get("projectSgddId") != null && !params.get("projectSgddId").equals("") && !params.get("projectSgddId").equals("undefined")) {
            qw.eq("project_sgdd_id", params.get("projectSgddId"));
        }
        //是否导入合同
        if (params.get("isContant") != null && !params.get("isContant").equals("") && !params.get("isContant").equals("undefined")) {
            qw.eq("is_contant", params.get("isContant"));
        }
        //关键字
        if (params.get("gjz") != null && !params.get("gjz").equals("") && !params.get("gjz").equals("undefined")) {
            //地点 建设单位 进度处理
            int id = 0;
            List<BusinessDictEntity> dic = businessDictDao.queryAll();
            for (BusinessDictEntity gjz : dic) {
                if(gjz.getName().equals(params.get("gjz"))){
                    id = gjz.getDictId();
                }
            }
            if (id == 0) {
                Consumer<QueryWrapper<BusinessProjectEntity>> consumer = qw2 -> {
                    qw2.like("project_year", params.get("gjz"));
                    qw2.or();
                    qw2.like("project", params.get("gjz"));
                    qw2.or().like("project_new_name", params.get("gjz")).or().like("project_photo_conut", params.get("gjz"));
                };
                qw.and(consumer);

            } else {
                int ids = id;
                Consumer<QueryWrapper<BusinessProjectEntity>> consumer = qw2 -> {
                    qw2.like("project_year", params.get("gjz")).or().like("project", params.get("gjz")).or().like("project_new_name", params.get("gjz"))
                            .or().like("project_photo_conut", params.get("gjz")).or().eq("project_sgdd_id", ids).or().eq("project_sgjd_id", ids)
                            .or().eq("project_jsdw_id", ids);
                };
                qw.and(consumer);
            }
        }

        qw.orderByDesc("project_id");

        //删除传入的数组处理
        *//*if (params.get("ids") != null && !params.get("ids").equals("") && !params.get("ids").equals("undefined")){

        }*//*
        IPage<BusinessProjectEntity> page = this.page(
                new Query<BusinessProjectEntity>().getPage(params),
                qw
        );*/


        IPage<BusinessProjectEntity> page = new Query<BusinessProjectEntity>().getPage(params);
        List<BusinessProjectEntity> list = baseMapper.quaryListNoPage(params);
        page.setRecords(list);
        return new PageUtils(page);
    }

    /**
     * 合同管理检索列表
     *
     * @param params
     * @return
     */
    @Override
    public List<BusinessProjectEntity> querySearchList(Map<String, Object> params) {
        return baseMapper.querySearchList(params);
    }

    @Override
    public List<BusinessProjectEntity> queryPerojectList(String ids) {
        String[] list = ids.split(",");
        return baseMapper.queryPerojectList(list);
    }

    /**
     * 更改合同标志为是
     * @param ids
     */
    @Override
    public void changeState(String[] ids) {
        baseMapper.changeState(ids);
    }

    /**
     * 更改合同标志为否
     * @param ids
     */
    @Override
    public void channelState(String[] ids) {
        baseMapper.channelState(ids);
    }

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

    /**
     * 立项负责工人idList
     * @return
     */
    @Override
    public List<Integer> ddIdList() {
        return baseMapper.ddIdList();
    }

    /**
     * 获取所有的列表
     * @return
     */
    @Override
    public List<BusinessProjectEntity> allList() {
        return baseMapper.allList();
    }

    /**
     * 立项详情
     * @param id
     * @return
     */
    @Override
    public BusinessProjectEntity projectInfo(Integer id) {
        return baseMapper.projectInfo(id);
    }

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

}
