<?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.baseinfo.mapper.BasePlanInfoMapper">

    <resultMap type="BasePlanInfo" id="BasePlanInfoResult">
        <result property="planid"    column="planid"    />
        <result property="deviceid"    column="deviceid"    />
        <result property="planShiftId"    column="plan_shift_id"    />
        <result property="planStateId"    column="plan_state_id"    />
        <result property="startTime"    column="start_time"    />
        <result property="overTime"    column="over_time"    />
        <result property="status"    column="status"    />
        <result property="remark"    column="remark"    />
        <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="deviceName"    column="device_name"    />
        <result property="shiftname"    column="shiftname"    />
        <result property="groupname"    column="groupname"    />
        <association property="planDetailInfo"    column="planDetailInfo" javaType="BasePlandetailInfo" resultMap="BasePlanDetailInfoResult" />
    </resultMap>

    <resultMap type="BasePlandetailInfo" id="BasePlanDetailInfoResult">
        <result property="objid"    column="objid"    />
        <result property="materialId"    column="material_id"    />
        <result property="planAmount"    column="plan_amount"    />
        <result property="realAmount"    column="real_amount"    />
        <result property="unitId"    column="unit_id"    />
        <result property="groupId"    column="group_id"    />
    </resultMap>

    <sql id="selectBasePlanInfoVo">
        select planid, deviceid, plan_shift_id, plan_state_id, start_time, over_time, status, remark, create_by, create_time, update_by, update_time, del_flag from base_plan_info
    </sql>

    <select id="selectBasePlanInfoList" parameterType="BasePlanInfo" resultMap="BasePlanInfoResult">
        select t1.planid, t1.deviceid,t2.device_name, t1.plan_shift_id,t3.shiftname, t1.plan_state_id, t1.status, t1.remark, t1.start_time, t1.over_time, t1.create_by, t1.create_time, t1.update_by, t1.update_time, t1.del_flag from base_plan_info t1
        left join base_device_info t2 on t1.deviceid = t2.device_id
        left join base_shift_info t3 on t1.plan_shift_id = t3.shiftid
        <where>
            <if test="deviceid != null  and deviceid != ''"> and deviceid = #{deviceid}</if>
            <if test="planShiftId != null  and planShiftId != ''"> and plan_shift_id = #{planShiftId}</if>
            <if test="planStateId != null  and planStateId != ''"> and plan_state_id = #{planStateId}</if>
            <if test="status != null  and status != ''"> and status = #{status}</if>
        </where>
    </select>

    <select id="selectBasePlanInfoById" parameterType="String" resultMap="BasePlanInfoResult">
        <include refid="selectBasePlanInfoVo"/>
        where planid = #{planid}
    </select>

    <insert id="insertBasePlanInfo" parameterType="BasePlanInfo">
        insert into base_plan_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="planid != null">planid,</if>
            <if test="deviceid != null">deviceid,</if>
            <if test="planShiftId != null">plan_shift_id,</if>
            <if test="planStateId != null">plan_state_id,</if>
            <if test="startTime != null">start_time,</if>
            <if test="overTime != null">over_time,</if>
            <if test="status != null">status,</if>
            <if test="remark != null">remark,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="planid != null">#{planid},</if>
            <if test="deviceid != null">#{deviceid},</if>
            <if test="planShiftId != null">#{planShiftId},</if>
            <if test="planStateId != null">#{planStateId},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="overTime != null">#{overTime},</if>
            <if test="status != null">#{status},</if>
            <if test="remark != null">#{remark},</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>
         </trim>
    </insert>

    <update id="updateBasePlanInfo" parameterType="BasePlanInfo">
        update base_plan_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceid != null">deviceid = #{deviceid},</if>
            <if test="planShiftId != null">plan_shift_id = #{planShiftId},</if>
            <if test="planStateId != null">plan_state_id = #{planStateId},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="overTime != null">over_time = #{overTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</if>
            <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>
        </trim>
        where planid = #{planid}
    </update>

    <delete id="deleteBasePlanInfoById" parameterType="String">
        delete from base_plan_info where planid = #{planid}
    </delete>

    <delete id="deleteBasePlanInfoByIds" parameterType="String">
        delete from base_plan_info where planid in
        <foreach item="planid" collection="array" open="(" separator="," close=")">
            #{planid}
        </foreach>
    </delete>

    <select id="selectBasePlanInfoListByPlanID" parameterType="String" resultMap="BasePlanInfoResult">
        select t1.planid, t1.deviceid, t1.plan_shift_id, t1.plan_state_id, t1.start_time, t1.over_time,t1.status, t1.remark, t1.create_by, t1.create_time, t1.update_by, t1.update_time, t1.del_flag,t2.objid,t2.material_id,t2.plan_amount,t2.real_amount,t2.unit_id,t2.group_id,t3.groupname from base_plan_info
        t1 left join base_plandetail_info t2  on t1.planid = t2.planid
        left join base_group_info t3 on t2.group_id = t3.groupid
        where t1.planid = #{planid}
    </select>

</mapper>
