package com.ssm.manage.dao.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ssm.core.impl.BaseDaoImpl;
import com.ssm.manage.dao.SsmProjectDao;
import com.ssm.manage.entity.BaseProject;
import com.ssm.manage.entity.BaseProjectType;
import com.ssm.manage.entity.CooperateList;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.mapper.BaseProjectMapper;
import com.ssm.manage.mapper.CooperateListMapper;
import com.ssm.manage.query.ProjectRequest;
import com.ssm.manage.query.ProjectTypeRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmProjectTypeService;
import com.ssm.manage.utils.DateUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Component
public class SsmProjectDaoImpl extends BaseDaoImpl<BaseProject> implements SsmProjectDao {
    @Autowired
    private BaseProjectMapper mapper;

    @Autowired
    private CooperateListMapper cooperateListMapper;


    @Autowired
    private SsmProjectTypeService ssmProjectTypeService;
    @Override
    public BaseProjectMapper getMapper() {
        return mapper;
    }

    @Override
    public PageInfo<BaseProject> selectPage(ProjectRequest projectRequest) {
//        List<CooperateList> cooperateLists = cooperateListMapper.selectAll();
        if(null != projectRequest.getProjectTypeId()){
            //查询当前typeId下的所有子类
            BaseProjectType baseProjectType = new BaseProjectType();
            baseProjectType.setParentId(projectRequest.getProjectTypeId());
            List<Long> selectByEveryId = ssmProjectTypeService.selectByEveryId(baseProjectType);
            selectByEveryId.add(projectRequest.getProjectTypeId());
            if(projectRequest.getExport()==0){
                Example example = new Example(BaseProject.class);
                Example.Criteria criteria = example.createCriteria();
                example.setOrderByClause("project_num desc");
                if (StringUtils.isNotBlank(projectRequest.getProjectNum())){
                    criteria.andLike("projectNum","%" + projectRequest.getProjectNum() + "%");
                }
                if (StringUtils.isNotBlank(projectRequest.getProjectName())){
                    criteria.andLike("projectName","%" + projectRequest.getProjectName() + "%");
                }

                //todo 修改
                if(projectRequest.getSelectNumName()!=null){
                        Example.Criteria criteria2 = example.createCriteria();
                        criteria2.orLike("projectName","%"+projectRequest.getSelectNumName()+"%");
                        criteria2.orLike("projectNum","%"+projectRequest.getSelectNumName()+"%");
                        example.and(criteria2);
//                    criteria.andLike("projectName","%"+projectRequest.getSelectNumName()+"%");
//                    criteria.orLike("projectNum","%"+projectRequest.getSelectNumName()+"%");
                }
                if (projectRequest.getState() != null){
                    criteria.andEqualTo("state",projectRequest.getState());
                }

                if (projectRequest.getProjectTypeId() != null){
//                      Example.Criteria criteria2 = example.createCriteria();
//                    criteria2.orLike("projectName","%"+projectRequest.getSelectNumName()+"%");
//                    criteria2.orLike("projectNum","%"+projectRequest.getSelectNumName()+"%");
//                    example.and(criteria2);
                    criteria.andIn("projectTypeId",selectByEveryId);
                }
                if(projectRequest.getProjectTypeIds()!=null&&!projectRequest.getProjectTypeIds().trim().equals("")){
                    List<String> typeIds=new ArrayList<>();
                    typeIds= Arrays.asList(projectRequest.getProjectTypeIds().split(","));
                    criteria.orIn("projectTypeId",typeIds);
                }

                if (projectRequest.getStoreId() != null) {
                    criteria.andEqualTo("storeId", projectRequest.getStoreId());
                }
                if (projectRequest.getDirectConsumption() != null){
                    criteria.andEqualTo("directConsumption",projectRequest.getDirectConsumption());
                }
                if (projectRequest.getType() != null){
                    criteria.andBetween("founderDatetime", DateUtils.diffDay(new Date(), -7), new Date());
                }
//        if (projectRequest.getStoreIds() != null){
//            criteria.andIn("storeId", Arrays.asList(projectRequest.getStoreIds()));
//        }criteria.andXxxGreaterThanOrEqualTo(value)
                if (projectRequest.getIsCooperation() != null){
                    criteria.andIn("isCooperation", Arrays.asList(projectRequest.getIsCooperation()));
                }
                PageHelper.startPage(projectRequest.getPageNum(),projectRequest.getPageSize(),"project_num");
                List<BaseProject> list = selectByExample(example);
//                for (BaseProject baseProject : list) {
//                    if (cooperateLists != null && cooperateLists.size() > 0) {
//                        for (CooperateList cooperateList : cooperateLists) {
//                            if (baseProject.getCooperateId() != null && baseProject.getCooperateId().compareTo(cooperateList.getId()) == 0) {
//                                baseProject.setCooperateList(cooperateList);
//                                continue;
//                            }
//                        }
//                    }
//                }
                PageInfo<BaseProject> pageInfo = new PageInfo<>(list);
                return pageInfo;
            }else{
                List<Long> projectTypes = new ArrayList<>();
                ProjectTypeRequest projectRequest1 = new ProjectTypeRequest();
                projectRequest1.setProjectTypeName(projectRequest.getProjectTypeName());
                projectRequest1.setStoreId(projectRequest.getStoreId());
                List<ProjectTypeRequest> menuId = ssmProjectTypeService.getMenuId(projectRequest1);
                for (ProjectTypeRequest projectTypeRequest : menuId) {
                    projectTypes.add(projectTypeRequest.getId());
                }

                Example example = new Example(BaseProject.class);
                Example.Criteria criteria = example.createCriteria();
                example.setOrderByClause("project_num desc");
                if (StringUtils.isNotBlank(projectRequest.getProjectNum())){
                    criteria.andLike("projectNum","%" + projectRequest.getProjectNum() + "%");
                }
                if (StringUtils.isNotBlank(projectRequest.getProjectName())){
                    criteria.andLike("projectName","%" + projectRequest.getProjectName() + "%");
                }
                if (projectRequest.getState() != null){
                    criteria.andEqualTo("state",projectRequest.getState());
                }
                criteria.andIn("projectTypeId",projectTypes);
                if (projectRequest.getStoreId() != null) {
                    criteria.andEqualTo("storeId", projectRequest.getStoreId());
                }
                if (projectRequest.getDirectConsumption() != null){
                    criteria.andEqualTo("directConsumption",projectRequest.getDirectConsumption());
                }
                if (projectRequest.getType() != null){
                    criteria.andBetween("founderDatetime", DateUtils.diffDay(new Date(), -7), new Date());
                }
//        if (projectRequest.getStoreIds() != null){
//            criteria.andIn("storeId", Arrays.asList(projectRequest.getStoreIds()));
//        }criteria.andXxxGreaterThanOrEqualTo(value)
                if (projectRequest.getIsCooperation() != null){
                    criteria.andIn("isCooperation", Arrays.asList(projectRequest.getIsCooperation()));
                }
                PageHelper.startPage(projectRequest.getPageNum(),projectRequest.getPageSize(),"project_num");
                List<BaseProject> list = selectByExample(example);
//                for (BaseProject baseProject : list) {
//                    if (cooperateLists != null && cooperateLists.size() > 0) {
//                        for (CooperateList cooperateList : cooperateLists) {
//                            if (baseProject.getCooperateId() != null && baseProject.getCooperateId().compareTo(cooperateList.getId()) == 0) {
//                                baseProject.setCooperateList(cooperateList);
//                                continue;
//                            }
//                        }
//                    }
//                }
                PageInfo<BaseProject> pageInfo = new PageInfo<>(list);
                return pageInfo;
            }

        }
        else{
            Example example = new Example(BaseProject.class);
            Example.Criteria criteria = example.createCriteria();
            example.setOrderByClause("project_num desc");
            if (StringUtils.isNotBlank(projectRequest.getProjectNum())){
                criteria.andLike("projectNum","%" + projectRequest.getProjectNum() + "%");
            }
            if (StringUtils.isNotBlank(projectRequest.getProjectName())){
                criteria.andLike("projectName","%" + projectRequest.getProjectName() + "%");
            }
            if (projectRequest.getProjectTypeId()!=null){
                criteria.andEqualTo("projectTypeId", projectRequest.getProjectTypeId());
            }
            if (projectRequest.getState() != null){
                criteria.andEqualTo("state",projectRequest.getState());
            }
            if (projectRequest.getProjectTypeId() != null){
                //查询当前typeId下的所有子类
                BaseProjectType baseProjectType = new BaseProjectType();
                baseProjectType.setParentId(projectRequest.getProjectTypeId());
                List<Long> selectByEveryId = ssmProjectTypeService.selectByEveryId(baseProjectType);
                selectByEveryId.add(projectRequest.getProjectTypeId());
                criteria.andIn("projectTypeId",selectByEveryId);
            }
            if(projectRequest.getProjectTypeIds()!=null&&!projectRequest.getProjectTypeIds().trim().equals("")){
                List<String> typeIds=new ArrayList<>();
                typeIds= Arrays.asList(projectRequest.getProjectTypeIds().split(","));
                criteria.orIn("projectTypeId",typeIds);
            }
            if (projectRequest.getStoreId() != null) {
                criteria.andEqualTo("storeId", projectRequest.getStoreId());
            }
            if (projectRequest.getDirectConsumption() != null){
                criteria.andEqualTo("directConsumption",projectRequest.getDirectConsumption());
            }
            if (projectRequest.getType() != null){
                criteria.andBetween("founderDatetime", DateUtils.diffDay(new Date(), -7), new Date());
            }


            //todo 修改
            if(projectRequest.getSelectNumName()!=null){
                Example.Criteria criteria2 = example.createCriteria();
                criteria2.orLike("projectName","%"+projectRequest.getSelectNumName()+"%");
                criteria2.orLike("projectNum","%"+projectRequest.getSelectNumName()+"%");
                example.and(criteria2);
            }

//        if (projectRequest.getStoreIds() != null){
//            criteria.andIn("storeId", Arrays.asList(projectRequest.getStoreIds()));
//        }criteria.andXxxGreaterThanOrEqualTo(value)
            if (projectRequest.getIsCooperation() != null){
                criteria.andIn("isCooperation", Arrays.asList(projectRequest.getIsCooperation()));
            }
            PageHelper.startPage(projectRequest.getPageNum(),projectRequest.getPageSize(),"project_num");
            List<BaseProject> list = selectByExample(example);
//            for (BaseProject baseProject : list) {
//                if (cooperateLists != null && cooperateLists.size() > 0) {
//                    for (CooperateList cooperateList : cooperateLists) {
//                        if (baseProject.getCooperateId() != null && baseProject.getCooperateId().compareTo(cooperateList.getId()) == 0) {
//                            baseProject.setCooperateList(cooperateList);
//                            continue;
//                        }
//                    }
//                }
//            }
            PageInfo<BaseProject> pageInfo = new PageInfo<>(list);
            return pageInfo;
        }
    }

    @Override
    public PageInfo<BaseProject> repeatPage(ProjectRequest projectRequest) {

            PageHelper.startPage(projectRequest.getPageNum(),projectRequest.getPageSize(),"project_name");
            List<BaseProject> list = mapper.selectRepeatPage(projectRequest);
            PageInfo<BaseProject> pageInfo = new PageInfo<>(list);
            return pageInfo;

    }


    @Override
    public PageInfo<BaseProject> selectPageNew(ProjectRequest projectRequest)throws SmsException {
        Example example = new Example(BaseProject.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("project_num desc");
        if (StringUtils.isNotBlank(projectRequest.getProjectNum())){
            criteria.andLike("projectNum","%" + projectRequest.getProjectNum() + "%");
        }
        if (StringUtils.isNotBlank(projectRequest.getProjectName())){
            criteria.andLike("projectName","%" + projectRequest.getProjectName() + "%");
        }
        if (StringUtils.isNotBlank(projectRequest.getProjectTypeName())){
            criteria.andLike("projectTypeName","%" + projectRequest.getProjectTypeName() + "%");
        }
        if (projectRequest.getState() != null){
            criteria.andEqualTo("state",projectRequest.getState());
        }
        if (projectRequest.getProjectTypeId() != null){
            criteria.andEqualTo("projectTypeId",projectRequest.getProjectTypeId());
        }
        if (projectRequest.getStoreId() != null) {
            criteria.andEqualTo("storeId", projectRequest.getStoreId());
        }
        if (projectRequest.getDirectConsumption() != null){
            criteria.andEqualTo("directConsumption",projectRequest.getDirectConsumption());
        }
        if (projectRequest.getType() != null){
            criteria.andBetween("founderDatetime", DateUtils.diffDay(new Date(), -7), new Date());
        }
        if (projectRequest.getIsCooperation() != null){
            criteria.andIn("isCooperation", Arrays.asList(projectRequest.getIsCooperation()));
        }
        if (projectRequest.getProjectNature() != null){
            criteria.andIn("projectNature", Arrays.asList(projectRequest.getProjectNature()));
        }

        //111
        if(projectRequest.getPrice()!=null){
            Example.Criteria criteria2 = example.createCriteria();
            criteria2.orLike("price","%"+projectRequest.getPrice()+"%");
            criteria2.orLike("priceMember","%"+projectRequest.getPrice()+"%");
            example.and(criteria2);
        }

        /*Example.Criteria criteria3 = example.createCriteria();
        criteria3.orIsNull("unifiedDue");
        Example.Criteria criteria4 = example.createCriteria();
        criteria4.orEqualTo(criteria3).andIsNull("unifiedDue").andIsNull("salesDeadline");
        criteria4.orEqualTo(criteria3).andIsNotNull("unifiedDue").andGreaterThan("unifiedDue",new Date()).andIsNull("salesDeadline");
        criteria4.orEqualTo(criteria3).andIsNotNull("unifiedDue").andGreaterThan("unifiedDue",new Date()).andIsNotNull("salesDeadline").andGreaterThan("salesDeadline",new Date());
        example.and(criteria3);
        example.or(criteria4);
*/

        //criteria.andBetween("salesDeadline", DateUtils.diffDay(new Date(), -1), DateUtils.diffDay(new Date(), +1));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = null;

        //criteria.andNotBetween("salesDeadline",parse,new Date());
        List<BaseProject> listS = new ArrayList<>();
        PageHelper.startPage(projectRequest.getPageNum(),projectRequest.getPageSize());
        List<BaseProject> list = selectByExample(example);

        for (BaseProject baseProject : list) {
            Date unifiedDue = null;
            try {
                if (baseProject.getUnifiedDue() != null) {
                    unifiedDue = DateUtils.ymdhmsString2DateTime(baseProject.getUnifiedDue());
                }
            } catch (ParseException e) {
                throw new SmsException(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
            }
            if(unifiedDue == null ||  unifiedDue.after(new Date())){
                if (null == baseProject.getSalesDeadline()) {
                    listS.add(baseProject);
                } else {
                    String salesDeadline = baseProject.getSalesDeadline();
                    try {
                        parse = sdf.parse(salesDeadline);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    if (parse.after(new Date())) {
                        listS.add(baseProject);
                    }
                }
            }
        }
        PageInfo<BaseProject> pageInfo = new PageInfo<>(listS);
        return pageInfo;
    }



    @Override
    public PageInfo<BaseProject> selectPageNew1(ProjectRequest projectRequest)throws SmsException {
        if (projectRequest.getProjectNum()!=null&&!projectRequest.getProjectNum().trim().equals("")){
            projectRequest.setPageNum(1);
            projectRequest.setProjectNum("%" + projectRequest.getProjectNum().trim() + "%");
        }
        if (projectRequest.getProjectName()!=null&&!projectRequest.getProjectName().trim().equals("")){
            projectRequest.setProjectName("%" + projectRequest.getProjectName().trim() + "%");
        }
        if (projectRequest.getProjectTypeName()!=null&&!projectRequest.getProjectTypeName().trim().equals("")){
            projectRequest.setProjectTypeName("%" + projectRequest.getProjectTypeName().trim() + "%");
        }

        if (projectRequest.getIsCooperation() != null&&projectRequest.getIsCooperation().doubleValue()>=0){
            projectRequest.setNewdate1(DateUtils.diffDay(new Date(), -7));
            projectRequest.setNewdate2(new Date());
        }

        if(projectRequest.getPrice()!=null&&projectRequest.getPrice().doubleValue()>=0){
            projectRequest.setPrices(String.valueOf(projectRequest.getPrice()));
        }

        //获取第几页开始查询
        projectRequest.setPage((projectRequest.getPageNum()-1)*projectRequest.getPageSize());

        List<BaseProject> listS = mapper.selectPageNew(projectRequest);
        Integer count=mapper.selelctCount(projectRequest);

        Integer c=count%projectRequest.getPageSize();
        Integer pages=1;
        if(c==0){
            pages=count/projectRequest.getPageSize();
        }else{
            pages=count/projectRequest.getPageSize()+pages;
        }

        if(listS.size()==0&&count>listS.size()){
            projectRequest.setPage((pages-1)*projectRequest.getPageSize());
            listS = mapper.selectPageNew(projectRequest);
        }

        PageInfo<BaseProject> pageInfo = new PageInfo<>(listS);



        if(pages>1){
            if (projectRequest.getProjectName()!=null&&!projectRequest.getProjectName().trim().equals("")&&projectRequest.getPrice()!=null&&projectRequest.getPrice().doubleValue()>=0){
                pageInfo.setPageSize(count);
                pageInfo.setTotal(count);
            }else{
                pageInfo.setPages(pages);
                pageInfo.setHasNextPage(true);
                pageInfo.setNavigateLastPage(pages);
                pageInfo.setNextPage(pages);
                pageInfo.setTotal(count);
            }

        }

        return pageInfo;
    }





    @Override
    public PageInfo<BaseProject> selectPageType(ProjectRequest projectRequest) {
        Example example = new Example(BaseProject.class);
        Example.Criteria criteria = example.createCriteria();
        example.setOrderByClause("project_num desc");
        if (StringUtils.isNotBlank(projectRequest.getProjectNum())){
            criteria.andLike("projectNum","%" + projectRequest.getProjectNum() + "%");
        }
        if (StringUtils.isNotBlank(projectRequest.getProjectName())){
            criteria.andLike("projectName","%" + projectRequest.getProjectName() + "%");
        }
        if (StringUtils.isNotBlank(projectRequest.getProjectTypeName())){
            criteria.andLike("projectTypeName","%" + projectRequest.getProjectTypeName() + "%");
        }
        if (projectRequest.getState() != null){
            criteria.andEqualTo("state",projectRequest.getState());
        }
        if (projectRequest.getProjectTypeId() != null){
            criteria.andEqualTo("project_type_id",projectRequest.getProjectTypeId());
        }
        if (projectRequest.getStoreId() != null) {
            criteria.andEqualTo("storeId", projectRequest.getStoreId());
        }
        if (projectRequest.getDirectConsumption() != null){
            criteria.andEqualTo("directConsumption",projectRequest.getDirectConsumption());
        }
        if (projectRequest.getType() != null){
            criteria.andBetween("founderDatetime", DateUtils.diffDay(new Date(), -7), new Date());
        }
//        if (projectRequest.getStoreIds() != null){
//            criteria.andIn("storeId", Arrays.asList(projectRequest.getStoreIds()));
//        }
        if (projectRequest.getIsCooperation() != null){
            criteria.andIn("isCooperation", Arrays.asList(projectRequest.getIsCooperation()));
        }

        PageHelper.startPage(projectRequest.getPageNum(),projectRequest.getPageSize());
        List<BaseProject> list = selectByExample(example);
        PageInfo<BaseProject> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public List<BaseProject> selectOneim(BaseProject projectRequest) {
        Example example = new Example(BaseProject.class);
        Example.Criteria criteria = example.createCriteria();
        if (projectRequest.getProjectNum() != null){
            criteria.andEqualTo("projectNum",projectRequest.getProjectNum());
        }
        if (projectRequest.getStoreId() != null) {
            criteria.andEqualTo("storeId", projectRequest.getStoreId());
        }
        List<BaseProject> list = selectByExample(example);
        return list;
    }

    @Override
    public List<BaseProject> selectOneims(BaseProject projectRequest) {
        Example example = new Example(BaseProject.class);
        Example.Criteria criteria = example.createCriteria();
        if (projectRequest.getId() != null){
            criteria.andEqualTo("id",projectRequest.getId());
        }
//        if (projectRequest.getStoreId() != null) {
//            criteria.andEqualTo("storeId", projectRequest.getStoreId());
//        }
        List<BaseProject> list = selectByExample(example);
        return list;
    }


    @Override
    public List<BaseProject> selectOneimp(BaseProject projectRequest) {
        Example example = new Example(BaseProject.class);
        Example.Criteria criteria = example.createCriteria();
        if (projectRequest.getProjectNum() != null){
            criteria.andEqualTo("projectNum",projectRequest.getProjectNum());
        }
        if (projectRequest.getStoreId() != null) {
            criteria.andEqualTo("storeId", projectRequest.getStoreId());
        }
        List<BaseProject> list = selectByExample(example);
        return list;
    }

    @Override
    public int insertBatch(List<BaseProject> list) {
        /*int i=0;
        for(BaseProject baseProject:list){
            ProjectRequest projectRequest=new ProjectRequest();
            BeanUtils.copyProperties(baseProject,projectRequest);
            i= mapper.insertBatch1(projectRequest);
        }
        return i;*/
        for (BaseProject baseProject : list) {
            if(baseProject.getSalesAchievement()==null){
                baseProject.setSalesAchievement(BigDecimal.ZERO);
            }
            if(baseProject.getSalesCommission()==null){
                baseProject.setSalesCommission(BigDecimal.ZERO);
            }
            if(baseProject.getCardSalesCommission()==null){
                baseProject.setCardSalesCommission(BigDecimal.ZERO);
            }
            if(baseProject.getCardSalesAchievement()==null){
                baseProject.setCardSalesAchievement(BigDecimal.ZERO);
            }
        }
        return mapper.insertBatch(list);
    }

    @Override
    public List<BaseProject> selectUpdate(List<BaseProject> baseProjects) {
        return mapper.selectUpdate(baseProjects);
    }
}
