<?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.frequency.worksreport.mapper.WorksReportMapper">

    <resultMap type="com.ruoyi.frequency.worksreport.entity.WorksReport" id="WorksReportResult">
        <result property="id"    column="id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="userType"    column="user_type"    />
        <result property="userId"    column="user_id"    />
        <result property="worksId"    column="works_id"    />
        <result property="reason"    column="reason"    />
        <result property="content"    column="content"    />
        <result property="images"    column="images"    />
        <result property="result"    column="result"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    
    <sql id="selectWorksReportVo">
        select
            t.id ,
            t.create_by ,
            t.create_time ,
            t.update_by ,
            t.update_time ,
            t.del_flag ,
            t.user_type ,
            t.user_id ,
            t.works_id ,
            t.reason ,
            t.content ,
            t.images ,
            t.result ,
            t.remark
        from t_works_report AS t
                 LEFT JOIN t_store s on t.user_id = s.id and t.user_type = 2
                 LEFT JOIN t_customer c on t.user_id = c.id and t.user_type = 1
                 LEFT JOIN t_sale sa on t.user_id = sa.id and t.user_type = 3
                 LEFT JOIN t_works w on t.works_id = w.id
    </sql>

    <select id="selectWorksReportList" parameterType="com.ruoyi.frequency.worksreport.entity.WorksReport" resultMap="WorksReportResult">
        <include refid="selectWorksReportVo"/>
        <where> AND t.del_flag != 2
            <if test="userType != null "> and t.user_type = #{userType}</if>
            <if test="userId != null "> and t.user_id = #{userId}</if>
            <if test="worksId != null "> and t.works_id = #{worksId}</if>
            <if test="reason != null  and reason != ''"> and t.reason = #{reason}</if>
            <if test="content != null  and content != ''"> and t.content = #{content}</if>
            <if test="images != null  and images != ''"> and t.images = #{images}</if>
            <if test="result != null "> and t.result = #{result}</if>
            <if test="delFlag != null and delFlag != ''"> and t.del_flag = #{delFlag}</if>
            <if test="userInfo != null and userInfo.nickname != null  and userInfo.nickname != '' ">
                and (
                s.nickname like concat('%', #{userInfo.nickname}, '%')
                or
                c.nickname like concat('%', #{userInfo.nickname}, '%')
                or
                sa.nickname like concat('%', #{userInfo.nickname}, '%')
                )

            </if>
            <if test="userInfo != null and  userInfo.mobile != null and  userInfo.mobile != '' ">
                and (
                s.mobile like concat('%', #{userInfo.mobile}, '%')
                or
                c.mobile like concat('%', #{userInfo.mobile}, '%')
                or
                sa.mobile like concat('%', #{userInfo.mobile}, '%')
                )
            </if>
            <if test="beginTime != null ">
                and  t.create_time >= #{beginTime}

            </if>
            <if test="endTime != null ">
                and  t.create_time &lt; #{endTime}
            </if>
            <if test="works != null and  works.title != null and  works.title != '' ">
                and w.title like concat('%', #{works.title}, '%')
            </if>
        </where>
        order by t.create_time desc
    </select>

    <select id="selectWorksReportById" parameterType="Long" resultMap="WorksReportResult">
        <include refid="selectWorksReportVo"/>
        where t.id = #{id}
    </select>

    <insert id="insertWorksReport" parameterType="com.ruoyi.frequency.worksreport.entity.WorksReport">
        insert into t_works_report
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="userType != null">user_type,</if>
            <if test="userId != null">user_id,</if>
            <if test="worksId != null">works_id,</if>
            <if test="reason != null">reason,</if>
            <if test="content != null">content,</if>
            <if test="images != null">images,</if>
            <if test="result != null">result,</if>
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="userType != null">#{userType},</if>
            <if test="userId != null">#{userId},</if>
            <if test="worksId != null">#{worksId},</if>
            <if test="reason != null">#{reason},</if>
            <if test="content != null">#{content},</if>
            <if test="images != null">#{images},</if>
            <if test="result != null">#{result},</if>
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>

    <update id="updateWorksReport" parameterType="com.ruoyi.frequency.worksreport.entity.WorksReport">
        update t_works_report
        <trim prefix="SET" suffixOverrides=",">
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="userType != null">user_type = #{userType},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="worksId != null">works_id = #{worksId},</if>
            <if test="reason != null">reason = #{reason},</if>
            <if test="content != null">content = #{content},</if>
            <if test="images != null">images = #{images},</if>
            <if test="result != null">result = #{result},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>
</mapper>
                                                                                                                        