<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dayou.mapper.ProductionOutWarehouseMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.ProductionOutWarehouse">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="production_fund_id" property="productionFundId" />
        <result column="real_amount" property="realAmount" />
        <result column="remark" property="remark" />
        <result column="apply_id" property="applyId" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        created,
        modified,
        production_fund_id, real_amount, remark, apply_id, state, checker_id, check_time
    </sql>

    <select id="getList" parameterType="java.lang.Long" resultType="com.dayou.vo.ProductionOutWarehouseApplyVO">
        SELECT
            pow.*,
            u.name AS applyName,
            u1.NAME AS departmentChecker,
            u2.name as financeChecker
        FROM
            `production_out_warehouse` pow
                LEFT JOIN user u ON u.id = pow.apply_id
                LEFT JOIN user u1 ON u1.id = pow.department_checker_id
                left join user u2 on u2.id = pow.finance_checker_id
        WHERE
            pow.deleted = 0 and pow.production_fund_id = #{productionFundId}
        order by pow.created DESC
    </select>

    <select id="getListByBizTypeProductionId"  resultType="com.dayou.vo.ProductionOutWarehouseApplyVO">
        SELECT
            pow.*,
            u.name AS applyName,
            u1.NAME AS departmentChecker,
            u2.name as financeChecker
        FROM
            `production_out_warehouse` pow
                LEFT JOIN user u ON u.id = pow.apply_id
                LEFT JOIN user u1 ON u1.id = pow.department_checker_id
                left join user u2 on u2.id = pow.finance_checker_id
        WHERE
            pow.deleted = 0 and pow.business_type = #{businessType} and pow.production_id = #{productionId}
        order by pow.created DESC
    </select>

    <select id="getPersonalByOrderId" parameterType="java.lang.Long" resultType="com.dayou.vo.ProductionOutWarehouseApplyVO">
        SELECT
            pow.*,
            u.name AS applyName,
            u1.NAME AS departmentChecker,
            u2.name as financeChecker
        FROM
            `production_out_warehouse` pow
                LEFT JOIN user u ON u.id = pow.apply_id
                LEFT JOIN user u1 ON u1.id = pow.department_checker_id
                left join user u2 on u2.id = pow.finance_checker_id
        WHERE
            pow.deleted = 0 and pow.business_type = 'PERSONAL_BUSINESS' and pow.order_id = #{orderId}
        order by pow.created DESC
    </select>
    <select id="getPage" parameterType="com.dayou.entity.ProductionOutWarehouse" resultType="com.dayou.entity.ProductionOutWarehouse">
        SELECT
        pow.*,
        pow.business_type,
        pf.standard_amount,
        pf.real_amount AS xRealAmount,
        u.NAME AS applyName,
        u1.NAME AS departmentChecker,
        u2.name as financeChecker
        FROM
        `production_out_warehouse` pow
        LEFT JOIN production_fund pf ON pf.id = pow.production_fund_id
        LEFT JOIN user u ON u.id = pow.apply_id
        LEFT JOIN user u1 ON u1.id = pow.department_checker_id
        left join user u2 on u2.id = pow.finance_checker_id
        WHERE
        pow.deleted = 0
        <if test="vo!=null and vo.nCode!=null and (vo.nCode=='REPORT_OUT_DEPARTMENT_CHECK' or vo.nCode=='LETTER_OUT_DEPARTMENT_CHECK')">
            and pow.department_check_state is null
        </if>
        <if test="vo!=null and vo.nCode!=null and (vo.nCode=='REPORT_OUT_FINANCE_CHECK' or vo.nCode=='LETTER_OUT_FINANCE_CHECK')">
            and (pow.department_check_state = '审核通过' or pow.department_check_state is null)
            and pow.finance_check_state is null
        </if>
        <if test="keyword!=null and keyword!='' ">
            and (
             pow.report_no like concat('%',#{keyword},'%')
            or pf.standard_amount like concat('%',#{keyword},'%')
            or pf.real_amount like concat('%',#{keyword},'%')
            or u.NAME like concat('%',#{keyword},'%')
            or u1.NAME like concat('%',#{keyword},'%')
            or u2.NAME like concat('%',#{keyword},'%')
            or pow.department_check_state like concat('%',#{keyword},'%')
            or pow.finance_check_state like concat('%',#{keyword},'%')
            )
        </if>
        <if test="vo!=null and vo.businessType!=null and vo.businessType!=''">
            and pow.business_type = #{vo.businessType}
        </if>
        ORDER BY
            pow.created DESC
    </select>
</mapper>
