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

import cn.estsh.i3plus.ext.mes.api.base.bu3.ISxWorkLicenseService;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesWorkLicense;
import cn.estsh.i3plus.ext.mes.pojo.bean.MesWorkLicenseTravel;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesWorkLicenseRepository;
import cn.estsh.i3plus.ext.mes.pojo.repository.MesWorkLicenseTravelRepository;
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.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.impp.framework.boot.exception.ImppExceptionBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
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.List;

/**
 * @author wangjie
 * @version 1.0
 * @date 2021/1/13 9:28
 **/
@Slf4j
@Service
public class SxWorkLicenseService implements ISxWorkLicenseService {

    @Autowired
    private MesWorkLicenseRepository workLicenseRepository;

    @Autowired
    private MesWorkLicenseTravelRepository workLicenseTravelRepository;

    @Override
    public ListPager<MesWorkLicense> queryWorkLicenseByPager(MesWorkLicense workLicense, Pager pager) {
        DdlPackBean hqlPack = MesExtHqlPack.getWorkLicense(workLicense);
        pager = PagerHelper.getPager(pager, workLicenseRepository.findByHqlWhereCount(hqlPack));
        hqlPack.setOrderByStr(workLicense.orderBy());
        List<MesWorkLicense> workLicenseList = workLicenseRepository.findByHqlWherePage(hqlPack, pager);
        return new ListPager(workLicenseList, pager);
    }

    @Override
    public MesWorkLicense insertWorkLicense(MesWorkLicense workLicense) {
        MesWorkLicense workLicenseDb = workLicenseRepository.getByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.WORK_LICENSE_CODE},
                new Object[]{workLicense.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), workLicense.getWorkLicenseCode()});
        if (null != workLicenseDb && CommonEnumUtil.IS_VAILD.VAILD.getValue() == workLicenseDb.getIsValid())
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_EXIT.getCode())
                    .setErrorDetail("上岗证编码[%s]记录已存在", workLicense.getWorkLicenseCode())
                    .build();
        if (null == workLicenseDb) workLicenseDb = new MesWorkLicense();
        workLicenseDb.setWorkLicenseCode(workLicense.getWorkLicenseCode());
        workLicenseDb.setValidityDate(workLicense.getValidityDate());
        workLicenseDb.setUserName(workLicense.getUserName());
        workLicenseDb.setRemark(workLicense.getRemark());
        workLicenseDb.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        if (StringUtils.isEmpty(workLicenseDb.getId())) {
            ConvertBean.serviceModelInitialize(workLicenseDb, workLicense.getCreateUser());
            workLicenseDb = workLicenseRepository.insert(workLicenseDb);
        }
        else {
            workLicenseDb.setIsValid(CommonEnumUtil.IS_VAILD.VAILD.getValue());
            ConvertBean.serviceModelUpdate(workLicenseDb, workLicense.getCreateUser());
            workLicenseDb = workLicenseRepository.save(workLicenseDb);
        }
        insertWorkLicenseTravel(workLicenseDb);
        return workLicenseDb;
    }

    @Override
    public MesWorkLicense updateWorkLicense(MesWorkLicense workLicense) {
        MesWorkLicense workLicenseDb = workLicenseRepository.getByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED, MesExtConstWords.WORK_LICENSE_CODE},
                new Object[]{workLicense.getOrganizeCode(), CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue(), workLicense.getWorkLicenseCode()});
        if (null == workLicenseDb)
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("上岗证编码[%s]记录不存在", workLicense.getWorkLicenseCode())
                    .build();
        workLicenseDb.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        workLicenseDb.setIsValid(CommonEnumUtil.IS_VAILD.VAILD.getValue());
        workLicenseDb.setWorkLicenseCode(workLicense.getWorkLicenseCode());
        workLicenseDb.setValidityDate(workLicense.getValidityDate());
        workLicenseDb.setUserName(workLicense.getUserName());
        workLicenseDb.setRemark(workLicense.getRemark());
        ConvertBean.serviceModelUpdate(workLicenseDb, workLicense.getModifyUser());
        insertWorkLicenseTravel(workLicenseDb);
        return workLicenseRepository.save(workLicenseDb);
    }

    @Override
    public void updateWorkLicenseValidById(String idStr, Integer status, String userName) {
        if (status != CommonEnumUtil.IS_VAILD.INVAILD.getValue() && status != CommonEnumUtil.IS_VAILD.VAILD.getValue()) {
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION.getCode())
                    .setErrorDetail("启用，禁用枚举状态值【%s】未被定义", status)
                    .build();
        }
        MesWorkLicense workLicenseDb = workLicenseRepository.getById(Long.parseLong(idStr));
        if (null == workLicenseDb)
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("上岗证配置ID[%s]对应的记录不存在", idStr)
                    .build();
        workLicenseDb.setIsValid(status);
        workLicenseDb.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        ConvertBean.serviceModelUpdate(workLicenseDb, userName);
        workLicenseRepository.save(workLicenseDb);
        insertWorkLicenseTravel(workLicenseDb);
    }

    @Override
    public void deleteWorkLicenseById(String idStr, String userName) {
        MesWorkLicense workLicenseDb = workLicenseRepository.getById(Long.parseLong(idStr));
        if (null == workLicenseDb)
            throw ImppExceptionBuilder.newInstance()
                    .setSystemID(CommonEnumUtil.SOFT_TYPE.MES.getCode())
                    .setErrorCode(ImppExceptionEnum.VARIFY_EXCEPTION_DATA_NOT_EXIT.getCode())
                    .setErrorDetail("上岗证配置ID[%s]对应的记录不存在", idStr)
                    .build();
        workLicenseDb.setIsDeleted(CommonEnumUtil.TRUE_OR_FALSE.TRUE.getValue());
        workLicenseDb.setSystemSyncStatus(MesExtEnumUtil.IF_SYNC_STATUS.NO_SYNC.getValue());
        ConvertBean.serviceModelUpdate(workLicenseDb, userName);
        workLicenseRepository.save(workLicenseDb);
    }

    @Override
    public List<MesWorkLicense> getWorkLicenseAllList(String organizeCode) {
        return workLicenseRepository.findByProperty(
                new String[]{MesExtConstWords.ORGANIZE_CODE, MesExtConstWords.IS_DELETED},
                new Object[]{organizeCode, CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue()});
    }

    @Override
    public MesWorkLicenseTravel insertWorkLicenseTravel(MesWorkLicense workLicense) {
        MesWorkLicenseTravel workLicenseTravel = new MesWorkLicenseTravel();
        BeanUtils.copyProperties(workLicense, workLicenseTravel, MesExtConstWords.ID, MesExtConstWords.CREATE_USER, MesExtConstWords.CREATE_DATE_TIME, MesExtConstWords.MODIFY_USER, MesExtConstWords.MODIFY_DATE_TIME);
        ConvertBean.serviceModelUpdate(workLicenseTravel, workLicense.getModifyUser());
        return workLicenseTravelRepository.insert(workLicenseTravel);
    }

    @Override
    public ListPager<MesWorkLicenseTravel> queryWorkLicenseTravelByPager(MesWorkLicenseTravel workLicenseTravel, Pager pager) {
        DdlPackBean hqlPack = MesExtHqlPack.getWorkLicenseTravel(workLicenseTravel);
        pager = PagerHelper.getPager(pager, workLicenseTravelRepository.findByHqlWhereCount(hqlPack));
        hqlPack.setOrderByStr(workLicenseTravel.orderBy());
        List<MesWorkLicenseTravel> workLicenseTravelList = workLicenseTravelRepository.findByHqlWherePage(hqlPack, pager);
        if(!CollectionUtils.isEmpty(workLicenseTravelList)){
            for (MesWorkLicenseTravel mesWorkLicenseTravel : workLicenseTravelList) {
                mesWorkLicenseTravel.setIsValidStr(MesExtEnumUtil.IS_ENABLE.valueOfDescription(mesWorkLicenseTravel.getIsValid()));
            }
        }
        return new ListPager(workLicenseTravelList, pager);
    }


}
