package cn.estsh.i3plus.ext.mes.apiservice.serviceimpl.base;

import cn.estsh.i3plus.ext.mes.api.base.IProjectExtService;
import cn.estsh.i3plus.ext.mes.apiservice.dao.IOnSiteOrderDetailDao;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesOnSiteOrderDetail;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProcedureProject;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesProjectSort;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesOnSiteOrderDetailRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesProcedureProjectRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesProjectSortRepository;
import cn.estsh.i3plus.ext.mes.pojo.sqlpack.MesExtHqlPack;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtConstWords;
import cn.estsh.i3plus.ext.mes.pojo.util.MesExtEnumUtil;
import cn.estsh.i3plus.platform.common.convert.ConvertBean;
import cn.estsh.i3plus.platform.common.exception.ImppExceptionEnum;
import cn.estsh.i3plus.platform.common.tool.TimeTool;
import cn.estsh.i3plus.pojo.base.bean.DdlPackBean;
import cn.estsh.i3plus.pojo.base.bean.ListPager;
import cn.estsh.i3plus.pojo.base.common.Pager;
import cn.estsh.i3plus.pojo.base.common.PagerHelper;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import cn.estsh.i3plus.pojo.base.tool.HqlPack;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

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

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Service
public class ProjectExtService implements IProjectExtService {

    @Autowired
    private MesProjectSortRepository mesProjectSortRDao;

    @Autowired
    private MesProcedureProjectRepository mesProcedureProjectRDao;

    @Autowired
    private IOnSiteOrderDetailDao onSiteOrderDetailDao;

    @Autowired
    private MesOnSiteOrderDetailRepository mesOnSiteOrderDetailRDao;

    @Override
    public MesProjectSort insertMesProjectSort(MesProjectSort mesProjectSort) {
        //数据唯一性校验
        StringBuffer whereStr = new StringBuffer();

        // 封装唯一校验查询条件
        HqlPack.getStringEqualPack(mesProjectSort.getOrganizeCode(), "organizeCode", whereStr);
        HqlPack.getStringEqualPack(mesProjectSort.getProjectSortNo(), "projectSortNo", whereStr);

        if (mesProjectSortRDao.isExitByHqlWhere(whereStr.toString())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("项目分类代码【%s】在项目分类信息中已存在",mesProjectSort.getProjectSortNo())
                    .setErrorSolution("请更换新增参数")
                    .build();
        }

        ConvertBean.serviceModelInitialize(mesProjectSort, AuthUtil.getSessionUser().getUserName());
        return mesProjectSortRDao.insert(mesProjectSort);
    }

    @Override
    public void updateMesProjectSort(MesProjectSort mesProjectSort, String userName) {
        //数据唯一性校验
        StringBuffer whereStr = new StringBuffer();

        // 封装唯一校验查询条件
        HqlPack.getNumNOEqualPack(mesProjectSort.getId(), "id", whereStr);
        HqlPack.getStringEqualPack(mesProjectSort.getOrganizeCode(), "organizeCode", whereStr);
        HqlPack.getStringEqualPack(mesProjectSort.getProjectSortNo(), "projectSortNo", whereStr);

        if (mesProjectSortRDao.isExitByHqlWhere(whereStr.toString())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("项目分类代码【%s】在项目分类信息中已存在",mesProjectSort.getProjectSortNo())
                    .setErrorSolution("请更换修改参数")
                    .build();
        }
        ConvertBean.serviceModelUpdate(mesProjectSort, userName);

        mesProjectSortRDao.save(mesProjectSort);
    }

    @Override
    public ListPager<MesProjectSort> queryMesProjectSortByPager(MesProjectSort mesProjectSort, Pager pager) {
        if (mesProjectSort == null) {
            pager = PagerHelper.getPager(pager, mesProjectSortRDao.listCount());
            return new ListPager(mesProjectSortRDao.listPager(pager), pager);
        } else {
            DdlPackBean hqlPack = MesExtHqlPack.getMesProjectSort(mesProjectSort);
            hqlPack.setOrderByStr(mesProjectSort.orderBy());
            pager = PagerHelper.getPager(pager, mesProjectSortRDao.findByHqlWhereCount(hqlPack));
            return new ListPager(mesProjectSortRDao.findByHqlWherePage(hqlPack , pager), pager);
        }
    }

    @Override
    public int updateMesProjectSortStatus(String[] ids, int status, String userName) {
        StringBuffer wheres = new StringBuffer();
        HqlPack.getInPack(String.join(",", ids), "id", wheres);

        int num = mesProjectSortRDao.updateByHqlWhere(
                wheres.toString(),
                new String[]{"isValid", "modifyUser", "modifyDatetime"},
                new Object[]{status, userName, TimeTool.getNowTime(true)}
        );
        return num;
    }

    @Override
    public MesProcedureProject insertMesProcedureProject(MesProcedureProject mesProcedureProject) {
        //数据唯一性校验
        StringBuffer whereStr = new StringBuffer();

        // 封装唯一校验查询条件
        HqlPack.getStringEqualPack(mesProcedureProject.getOrganizeCode(), "organizeCode", whereStr);
        HqlPack.getStringEqualPack(mesProcedureProject.getProjectSortNo(), "projectSortNo", whereStr);
        HqlPack.getStringEqualPack(mesProcedureProject.getProjectCode(), "projectCode", whereStr);

        if (mesProcedureProjectRDao.isExitByHqlWhere(whereStr.toString())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("项目分类代码【%s】和项目代码【%s】在过程项目信息中已存在",
                            mesProcedureProject.getProjectSortNo(),mesProcedureProject.getProjectCode())
                    .setErrorSolution("请更换新增参数")
                    .build();
        }

        ConvertBean.serviceModelInitialize(mesProcedureProject, AuthUtil.getSessionUser().getUserName());
        return mesProcedureProjectRDao.insert(mesProcedureProject);
    }

    @Override
    public void updateMesProcedureProject(MesProcedureProject mesProcedureProject, String userName) {
        //数据唯一性校验
        StringBuffer whereStr = new StringBuffer();

        // 封装唯一校验查询条件
        HqlPack.getNumNOEqualPack(mesProcedureProject.getId(), "id", whereStr);
        HqlPack.getStringEqualPack(mesProcedureProject.getOrganizeCode(), "organizeCode", whereStr);
        HqlPack.getStringEqualPack(mesProcedureProject.getProjectSortNo(), "projectSortNo", whereStr);
        HqlPack.getStringEqualPack(mesProcedureProject.getProjectCode(), "projectCode", whereStr);

        if (mesProcedureProjectRDao.isExitByHqlWhere(whereStr.toString())) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("项目分类代码【%s】和项目代码【%s】在过程项目信息中已存在",
                            mesProcedureProject.getProjectSortNo(),mesProcedureProject.getProjectCode())
                    .setErrorSolution("请更换修改参数")
                    .build();
        }
        ConvertBean.serviceModelUpdate(mesProcedureProject, userName);

        mesProcedureProjectRDao.save(mesProcedureProject);
    }

    @Override
    public ListPager<MesProcedureProject> queryMesProcedureProjectByPager(MesProcedureProject mesProcedureProject, Pager pager) {
        if (mesProcedureProject == null) {
            pager = PagerHelper.getPager(pager, mesProcedureProjectRDao.listCount());
            return new ListPager(mesProcedureProjectRDao.listPager(pager), pager);
        } else {
            DdlPackBean hqlPack = MesExtHqlPack.getMesProcedureProject(mesProcedureProject);
            hqlPack.setOrderByStr(mesProcedureProject.orderBy());
            pager = PagerHelper.getPager(pager, mesProcedureProjectRDao.findByHqlWhereCount(hqlPack));
            return new ListPager(mesProcedureProjectRDao.findByHqlWherePage(hqlPack , pager), pager);
        }
    }

    @Override
    public int updateMesProcedureProjectStatus(String[] ids, int status, String userName) {
        StringBuffer wheres = new StringBuffer();
        HqlPack.getInPack(String.join(",", ids), "id", wheres);

        int num = mesProcedureProjectRDao.updateByHqlWhere(
                wheres.toString(),
                new String[]{"isValid", "modifyUser", "modifyDatetime"},
                new Object[]{status, userName, TimeTool.getNowTime(true)}
        );
        return num;
    }

    @Override
    public ListPager<MesOnSiteOrderDetail> queryMesOnSiteOrderDetailReportByPager(MesOnSiteOrderDetail mesOnSiteOrderDetail, Pager pager) {
        Integer total = onSiteOrderDetailDao.getMesOnSiteOrderDetailReportCount(mesOnSiteOrderDetail);
        if(total == 0){
            return new ListPager(new ArrayList(), pager);
        }
        pager = PagerHelper.getPager(pager, total);
        List<MesOnSiteOrderDetail> onSiteOrderDetails = onSiteOrderDetailDao.queryOnSiteOrderDetailReportByPager(mesOnSiteOrderDetail, pager);
        if (!CollectionUtils.isEmpty(onSiteOrderDetails)) {
            for (MesOnSiteOrderDetail detail : onSiteOrderDetails) {
                if (null == detail) {
                    continue;
                }
                if (!StringUtils.isEmpty(detail.getOrderStatus())) {
                    detail.setOrderStatusName(MesExtEnumUtil.MES_ORJ_ORDER_STATUS.valueOfDescription(detail.getOrderStatus()));
                }
                detail.setCheckResultName(detail.getCheckResult());
            }
        }
        return new ListPager(onSiteOrderDetails, pager);
    }

    @Override
    public void updateMesOnSiteOrderDetail(MesOnSiteOrderDetail mesOnSiteOrderDetail, String userName) {
        mesOnSiteOrderDetailRDao.updateByProperties(
                new String[]{"id"},
                new Object[]{mesOnSiteOrderDetail.getId()},
                new String[]{"maintainStrategy", "operateUser", "operateTime", MesExtConstWords.MODIFY_DATE_TIME, MesExtConstWords.MODIFY_USER},
                new Object[]{mesOnSiteOrderDetail.getMaintainStrategy(), userName,TimeTool.getNowTime(true),
                        TimeTool.getNowTime(true), userName});
    }
}


