package cn.iocoder.yudao.module.smartfactory.dal.mysql.staffsalary;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.module.smartfactory.controller.admin.staffsalary.vo.SalaryTotalVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.staffsalary.vo.StaffSalaryPageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.staffsalary.vo.StaffSalaryRespVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.factoryinfo.FactoryInfoDO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.staff.StaffDO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.staffsalary.StaffSalaryDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;
import java.util.Objects;

/**
 * 厂区员工工资 Mapper
 *
 * @author 符溶馨
 */
@Mapper
public interface StaffSalaryMapper extends BaseMapperX<StaffSalaryDO> {

    default PageResult<StaffSalaryRespVO> selectSalaryPage(StaffSalaryPageReqVO reqVO) {

        MPJLambdaWrapperX<StaffSalaryDO> query = new MPJLambdaWrapperX<StaffSalaryDO>()
                .selectAll(StaffSalaryDO.class)
                .selectAs(StaffDO::getNickName, StaffSalaryRespVO::getStaffName)
                .selectAs(StaffDO::getWorkTypeId, StaffSalaryRespVO::getWorkTypeId)
                .selectAs(FactoryInfoDO::getShortName, StaffSalaryRespVO::getFactoryName)
                .selectAs(StaffDO::getSalary, StaffSalaryRespVO::getSalary);

        query.leftJoin(StaffDO.class, "staff", StaffDO::getId, StaffSalaryDO::getStaffId);
        query.leftJoin(FactoryInfoDO.class, FactoryInfoDO::getId, StaffSalaryDO::getFactoryId);
        query.eqIfPresent(StaffSalaryDO::getStaffId, reqVO.getStaffId());
        query.eqIfPresent(StaffSalaryDO::getMonth, reqVO.getMonth());
        query.eqIfPresent(StaffSalaryDO::getFactoryId, reqVO.getFactoryId());
        query.inIfPresent(StaffSalaryDO::getFactoryId, reqVO.getFactoryIds());
        query.eqIfPresent(StaffSalaryDO::getStatus, reqVO.getStatus());
        query.eqIfPresent(StaffSalaryDO::getIsGrant, reqVO.getIsGrant());
        query.apply(Objects.nonNull(reqVO.getWorkTypeId()), "staff.work_type_id = {0} ", reqVO.getWorkTypeId());

        return selectJoinPage(reqVO, StaffSalaryRespVO.class, query);
    }

    SalaryTotalVO selectSalaryTotal(@Param("pageReqVO") StaffSalaryPageReqVO pageReqVO);

    default List<StaffSalaryRespVO> selectListSalarySubjectItems(StaffSalaryPageReqVO reqVO) {

        MPJLambdaWrapperX<StaffSalaryDO> query = new MPJLambdaWrapperX<StaffSalaryDO>();
        query.selectAs(StaffSalaryDO::getSalarySubjectItems, StaffSalaryRespVO::getSalarySubjectItems);

        query.leftJoin(StaffDO.class, "staff", StaffDO::getId, StaffSalaryDO::getStaffId);
        query.eqIfPresent(StaffSalaryDO::getMonth, reqVO.getMonth());
        query.eqIfPresent(StaffSalaryDO::getFactoryId, reqVO.getFactoryId());
        query.eqIfPresent(StaffSalaryDO::getStatus, reqVO.getStatus());
        query.apply(Objects.nonNull(reqVO.getWorkTypeId()), "staff.work_type_id = {0} ", reqVO.getWorkTypeId());

        return selectJoinList(StaffSalaryRespVO.class, query);
    }
}