package com.zt.plat.module.qms.resource.material.dal.mapper;

import cn.hutool.core.util.StrUtil;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.mybatis.core.mapper.BaseMapperX;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.qms.core.aspect.annotation.QmsPermission;
import com.zt.plat.module.qms.enums.QmsPermissionConstant;
import com.zt.plat.module.qms.resource.material.controller.vo.MaterialLifecyclePageReqVO;
import com.zt.plat.module.qms.resource.material.dal.dataobject.MaterialLifecycleDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.Arrays;
import java.util.List;

/**
 * 物料通用流程,物料验收、退换货 Mapper
 *
 * @author 后台管理
 */
@Mapper
public interface MaterialLifecycleMapper extends BaseMapperX<MaterialLifecycleDO> {

    @QmsPermission(moduleDataRoleCodes = QmsPermissionConstant.ADMIN_ROLE + "," + QmsPermissionConstant.MATERIAL_ADMIN_ROLE)
    default PageResult<MaterialLifecycleDO> selectPage(MaterialLifecyclePageReqVO reqVO) {
        String businessType = reqVO.getBusinessType();

        LambdaQueryWrapperX<MaterialLifecycleDO> wrapper = new LambdaQueryWrapperX<MaterialLifecycleDO>()
                .likeIfPresent(MaterialLifecycleDO::getTitle, reqVO.getTitle())
                .eqIfPresent(MaterialLifecycleDO::getBusinessTypeCode, reqVO.getBusinessTypeCode())
                .likeIfPresent(MaterialLifecycleDO::getApplyUser, reqVO.getApplyUser())
                .eqIfPresent(MaterialLifecycleDO::getApplyUserId, reqVO.getApplyUserId())
                .likeIfPresent(MaterialLifecycleDO::getApplyDepartment, reqVO.getApplyDepartment())
                .eqIfPresent(MaterialLifecycleDO::getApplyDepartmentId, reqVO.getApplyDepartmentId())
                .betweenIfPresent(MaterialLifecycleDO::getApplyTime, reqVO.getApplyTime())
                .likeIfPresent(MaterialLifecycleDO::getFormData, reqVO.getFormData())
                .eqIfPresent(MaterialLifecycleDO::getFlowInstanceId, reqVO.getFlowInstanceId())
                .eqIfPresent(MaterialLifecycleDO::getSubmitStatus, reqVO.getSubmitStatus())
                .eqIfPresent(MaterialLifecycleDO::getFlowStatus, reqVO.getFlowStatus())
                .eqIfPresent(MaterialLifecycleDO::getOperatorId, reqVO.getOperatorId())
                .likeIfPresent(MaterialLifecycleDO::getOperatorName, reqVO.getOperatorName())
                .eqIfPresent(MaterialLifecycleDO::getProgress, reqVO.getProgress())
                .eqIfPresent(MaterialLifecycleDO::getSystemDepartmentCode, reqVO.getSystemDepartmentCode())
                .eqIfPresent(MaterialLifecycleDO::getRemark, reqVO.getRemark())
                .betweenIfPresent(MaterialLifecycleDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(MaterialLifecycleDO::getId);
        if (StrUtil.isNotEmpty(businessType) && businessType.contains(",")) {
            String[] split = businessType.split(",");
            wrapper.inIfPresent(MaterialLifecycleDO::getBusinessType, Arrays.asList(split));
        } else {
            wrapper.eqIfPresent(MaterialLifecycleDO::getBusinessType, businessType);
        }
        return selectPage(reqVO, wrapper);

    }

}