<?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.ruoyi.oa.mapper.CmcDeclareMapper">
    
    <resultMap type="CmcDeclare" id="CmcDeclareResult">
        <result property="formId"    column="form_id"    />
        <result property="projectId"    column="project_id"    />
        <result property="projectLeader"    column="project_leader"    />
        <result property="userId"    column="user_id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="workType"    column="work_type"    />
        <result property="workItem"    column="work_item"    />
        <result property="workContent"    column="work_content"    />
        <result property="beginDate"    column="begin_date"    />
        <result property="endDate"    column="end_date"    />
        <result property="workLoad"    column="work_load"    />
        <result property="price"    column="price"    />
        <result property="coefficient"    column="coefficient"    />
        <result property="checkStatus"    column="check_status"    />
        <result property="auditStatus"    column="audit_status"    />
        <result property="submitTime"    column="submit_time"    />
        <result property="confirmStatus"    column="confirm_status"    />
        <association property="user"    javaType="SysUser"         resultMap="UserResult" />
        <association property="project"    javaType="CmcProject"         resultMap="CmcProjectResult" />
    </resultMap>

    <resultMap type="SysUser" id="UserResult">
        <result property="userId"    column="user_id"    />
        <result property="nickName"    column="nick_name"    />
    </resultMap>

    <sql id="selectCmcDeclareVo">
        select d.form_id, d.project_id, p.project_number, p.project_name, p.project_leader, d.user_id, u.dept_id, u.nick_name, d.work_type, d.work_item, d.work_content,
               d.begin_date, d.end_date, d.work_load, d.price, d.coefficient, d.check_status, d.audit_status, d.submit_time, d.confirm_status from cmc_declare as d
        left join sys_user as u on u.user_id = d.user_id
        left join cmc_project as p on p.project_id = d.project_id
    </sql>

    <resultMap type="CmcProject" id="CmcProjectResult">
        <result property="projectId"    column="project_id"    />
        <result property="projectNumber"    column="project_number"    />
        <result property="projectName"    column="project_name"    />
    </resultMap>

    <select id="selectCmcDeclareList" parameterType="CmcDeclare" resultMap="CmcDeclareResult">
        <include refid="selectCmcDeclareVo"/>
        <where>  
            <if test="projectId != null  and projectId != ''"> and d.project_id = #{projectId}</if>
            <if test="projectLeader != null"> and p.project_leader = #{projectLeader}</if>
            <if test="userId != null"> and d.user_id = #{userId}</if>
            <if test="deptId != null"> and u.dept_id = #{deptId}</if>
            <if test="workType != null  and workType != ''"> and d.work_type = #{workType}</if>
            <if test="workItem != null  and workItem != ''"> and d.work_item = #{workItem}</if>
            <if test="workContent != null  and workContent != ''"> and d.work_content = #{workContent}</if>
            <if test="beginDate != null "> and date_format(d.begin_date, '%y%m') = date_format(#{beginDate}, '%y%m')</if>
            <if test="endDate != null "> and date_format(d.end_date, '%y%m') = date_format(#{endDate}, '%y%m')</if>
            <if test="workLoad != null  and workLoad != ''"> and d.work_load = #{workLoad}</if>
            <if test="price != null "> and d.price = #{price}</if>
            <if test="coefficient != null  and coefficient != ''"> and d.coefficient = #{coefficient}</if>
            <if test="checkStatus != null  and checkStatus != ''"> and d.check_status = #{checkStatus}</if>
            <if test="auditStatus != null  and auditStatus != ''"> and d.audit_status = #{auditStatus}</if>
            <if test="submitTime != null "> and date_format(d.submit_time, '%y%m') = date_format(#{submitTime}, '%y%m')</if>
            <if test="confirmStatus != null  and confirmStatus != ''"> and d.confirm_status = #{confirmStatus}</if>
        </where>
        order by d.submit_time desc
    </select>
    
    <select id="selectCmcDeclareByFormId" parameterType="String" resultMap="CmcDeclareResult">
        <include refid="selectCmcDeclareVo"/>
        where d.form_id = #{formId}
    </select>
        
    <insert id="insertCmcDeclare" parameterType="CmcDeclare">
        insert into cmc_declare
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="formId != null">form_id,</if>
            <if test="projectId != null">project_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="workType != null">work_type,</if>
            <if test="workItem != null">work_item,</if>
            <if test="workContent != null">work_content,</if>
            <if test="beginDate != null">begin_date,</if>
            <if test="endDate != null">end_date,</if>
            <if test="workLoad != null">work_load,</if>
            <if test="price != null">price,</if>
            <if test="coefficient != null">coefficient,</if>
            <if test="checkStatus != null">check_status,</if>
            <if test="auditStatus != null">audit_status,</if>
            <if test="submitTime != null">submit_time,</if>
            <if test="confirmStatus != null">confirm_status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="formId != null">#{formId},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="workType != null">#{workType},</if>
            <if test="workItem != null">#{workItem},</if>
            <if test="workContent != null">#{workContent},</if>
            <if test="beginDate != null">#{beginDate},</if>
            <if test="endDate != null">#{endDate},</if>
            <if test="workLoad != null">#{workLoad},</if>
            <if test="price != null">#{price},</if>
            <if test="coefficient != null">#{coefficient},</if>
            <if test="checkStatus != null">#{checkStatus},</if>
            <if test="auditStatus != null">#{auditStatus},</if>
            <if test="submitTime != null">#{submitTime},</if>
            <if test="confirmStatus != null">#{confirmStatus},</if>
         </trim>
    </insert>

    <update id="updateCmcDeclare" parameterType="CmcDeclare">
        update cmc_declare
        <trim prefix="SET" suffixOverrides=",">
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="workType != null">work_type = #{workType},</if>
            <if test="workItem != null">work_item = #{workItem},</if>
            <if test="workContent != null">work_content = #{workContent},</if>
            <if test="beginDate != null">begin_date = #{beginDate},</if>
            <if test="endDate != null">end_date = #{endDate},</if>
            <if test="workLoad != null">work_load = #{workLoad},</if>
            <if test="price != null">price = #{price},</if>
            <if test="coefficient != null">coefficient = #{coefficient},</if>
            <if test="checkStatus != null">check_status = #{checkStatus},</if>
            <if test="auditStatus != null">audit_status = #{auditStatus},</if>
            <if test="submitTime != null">submit_time = #{submitTime},</if>
            <if test="confirmStatus != null">confirm_status = #{confirmStatus},</if>
        </trim>
        where form_id = #{formId}
    </update>

    <delete id="deleteCmcDeclareByFormId" parameterType="String">
        delete from cmc_declare where form_id = #{formId}
    </delete>

    <delete id="deleteCmcDeclareByFormIds" parameterType="String">
        delete from cmc_declare where form_id in 
        <foreach item="formId" collection="array" open="(" separator="," close=")">
            #{formId}
        </foreach>
    </delete>
</mapper>