<?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.bonus.material.ma.mapper.WarehousingMapper">
    <select id="getList" resultType="com.bonus.material.ma.domain.PutInStorageBean">
        SELECT
            psi.id as id,
            case when psi.put_in_type = 0 then '库存盘点入库'
            else ''
            end as putInType,
            psi.code as code,
            su.nick_name as modelName,
            psi.create_time as createDate,
            psi.remarks as remark,
            GROUP_CONCAT(DISTINCT mt1.type_name) AS kindName
        FROM ma_type_put_in_storage_info psi
        left join sys_user su on psi.creator = su.user_id
        left join ma_type_put_in_storage_details psd on psi.id = psd.info_id
        left join ma_type mt on psd.type_id = mt.type_id and mt.del_flag = '0'
        left join ma_type mt1 on mt.parent_id = mt1.type_id and mt1.del_flag = '0'
        left join ma_type mt2 ON mt1.parent_id = mt2.type_id and mt2.del_flag = '0'
        left join ma_type mt3 ON mt2.parent_id = mt3.type_id and mt3.del_flag = '0'
        WHERE 1=1
        <if test="keyWord != null and keyWord != ''">
            AND (
            psi.code LIKE CONCAT('%',#{keyWord},'%') OR
            su.nick_name LIKE CONCAT('%',#{keyWord},'%') OR
            psi.remarks LIKE CONCAT('%',#{keyWord},'%')
            )
        </if>
        GROUP BY psi.id
        ORDER BY psi.create_time DESC
    </select>
    <select id="selectMaSupplierInfoList" resultType="com.bonus.material.ma.domain.SupplierInfo">
        SELECT
            supplier_id as supplierId ,
            supplier as supplier
        FROM ma_supplier_info
    </select>

    <select id="selectByMaCode" resultType="java.lang.Integer">
        select count(*)
        from ma_machine
        <where>
            <if test="maCode != null ">and ma_code = #{maCode}</if>
        </where>
    </select>
    <select id="selectTaskNumByMonth" resultType="java.lang.Integer">
        select count(*) from ma_type_put_in_storage_info where DATE_FORMAT(create_time,'%y%m') = DATE_FORMAT(#{date},'%y%m')
    </select>

    <insert id="saveInfo" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO ma_type_put_in_storage_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="putInType != null and putInType != ''">put_in_type,</if>
            <choose>
                <when test="isCode and num != null and num != 0">
                    num,
                </when>
                <otherwise>
                    <if test="totalNum != null and totalNum != 0">
                        num,
                    </if>
                </otherwise>
            </choose>
<!--            <if test="typeId != null">type_id,</if>-->
            <if test="creator != null">creator,</if>
            <if test="code != null and code != ''">code,</if>
            <if test="remarks != null and remarks != ''">remarks,</if>
            create_time
        </trim>
        VALUES
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="putInType != null and putInType != ''">#{putInType},</if>
            <choose>
                <when test="isCode and num != null and num != 0">
                    #{num},
                </when>
                <otherwise>
                    <if test="totalNum != null and totalNum != 0">
                        #{totalNum},
                    </if>
                </otherwise>
            </choose>

            <if test="creator != null and creator != ''">#{creator},</if>
            <if test="code != null and code != ''">#{code},</if>
            <if test="remarks != null and remarks != ''">#{remarks},</if>
            sysdate()
        </trim>
    </insert>

    <insert id="saveDetails" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO ma_type_put_in_storage_details (
        <trim prefix="" suffixOverrides=",">
            <choose>
                <when test="isCode and num != null and num != 0">
                    num,
                </when>
                <otherwise>
                    <if test="putInStoreNum != null and putInStoreNum != 0">
                        num,
                    </if>
                </otherwise>
            </choose>
            <if test="infoId != null and infoId != ''">
                info_id,
            </if>
            <if test="typeId != null and typeId != ''">
                type_id,
            </if>
            <if test="maId != null and maId != ''">
                ma_id,
            </if>
            <if test="remark != null and remark != ''">
                remarks,
            </if>
            <if test="maCode != null and maCode != ''">
                ma_code,
            </if>
        </trim>
        )
        VALUES (
        <choose>
            <when test="isCode and num != null and num != 0">
                1,
            </when>
            <otherwise>
                <if test="putInStoreNum != null and putInStoreNum != 0">
                    #{putInStoreNum},
                </if>
            </otherwise>
        </choose>
        <trim prefix="" suffixOverrides=",">
            <if test="infoId != null and infoId != ''">
                #{infoId},
            </if>
            <if test="typeId != null and typeId != ''">
                #{typeId},
            </if>
            <if test="maId != null and maId != ''">
                #{maId},
            </if>
            <if test="remark != null and remark != ''">
                #{remark},
            </if>
            <if test="maCode != null and maCode != ''">
                #{maCode},
            </if>
        </trim>
        )
    </insert>

    <insert id="insertMachine" useGeneratedKeys="true" keyProperty="maId">
        insert into ma_machine (
        <if test="typeId != null and typeId != '' ">type_id,</if>
        <if test="maCode != null and maCode != '' ">ma_code,</if>
        ma_status,
        <if test="buyPrice != null and buyPrice != ''">buy_price,</if>
        <if test="maVender != null and maVender != ''">ma_vender,</if>
        <if test="checkMan != null and checkMan != ''">check_man,</if>
        <if test="outFacCode != null and outFacCode != ''">out_fac_code,</if>
        <if test="thisCheckTime != null">this_check_time,</if>
        <if test="nextCheckTime != null">next_check_time,</if>
        create_time
        )values(
        <if test="typeId != null and typeId != ''">#{typeId},</if>
        <if test="maCode != null and maCode != ''">#{maCode},</if>
        1,
        <if test="buyPrice != null and buyPrice != ''">#{buyPrice},</if>
        <if test="maVender != null and maVender != ''">#{maVender},</if>
        <if test="checkMan != null and checkMan != ''">#{checkMan},</if>
        <if test="outFacCode != null and outFacCode != ''">#{outFacCode},</if>
        <if test="thisCheckTime != null">#{thisCheckTime},</if>
        <if test="nextCheckTime != null">#{nextCheckTime},</if>
        sysdate()
        )
    </insert>

    <select id="getDetails" resultType="com.bonus.material.ma.domain.PutInStorageBean">
        SELECT
        case when psi.put_in_type = 0 then '库存盘点入库'
        else ''
        end as putInType,
        psd.ma_code   as maCode,
        psd.num         as num,
        mt2.type_name    as typeName,
        mt.type_name     as typeModelName,
        su.nick_name     as modelName,
        psi.create_time as createDate,
        psd.remarks as remark
        FROM ma_type_put_in_storage_info psi
        LEFT JOIN ma_type_put_in_storage_details psd on psi.id = psd.info_id
        LEFT JOIN ma_type mt on mt.type_id = psd.type_id
        LEFT JOIN ma_type mt2 on mt2.type_id = mt.parent_id
        LEFT JOIN sys_user su on su.user_id = psi.creator
        where
        1 = 1
        <if test="infoId != null and infoId != ''">
            and psi.id = #{infoId}
        </if>
        <if test="keyWord != null and keyWord != ''">
            and (
            mt2.type_name like concat('%',#{keyWord},'%') or
            su.user_name like concat('%',#{keyWord},'%') or
            mt.type_name like concat('%',#{keyWord},'%') or
            psd.ma_code like concat('%',#{keyWord},'%')
            )
        </if>
    </select>
</mapper>