<?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.canteen.core.supermarket.mapper.SupermarketProductMapper">
    <resultMap type="com.bonus.canteen.core.supermarket.domain.SupermarketProduct" id="SupermarketProductResult">
        <result property="productId"                 column="product_id"                 />
        <result property="supermarketId"      column="supermarket_id"     />
        <result property="productName"        column="product_name"       />
        <result property="barCode"            column="bar_code"           />
        <result property="salePrice"          column="sale_price"         />
        <result property="salesMode"          column="sales_mode"         />
        <result property="areaId"             column="area_id"            />
        <result property="unitId"             column="unit_id"            />
        <result property="materialId"         column="material_id"        />
        <result property="prefPrice"          column="pref_price"         />
        <result property="qualityType"        column="quality_type"       />
        <result property="qualityNum"         column="quality_num"        />
        <result property="supplyCertificate"  column="supply_certificate" />
        <result property="productRemark"      column="product_remark"     />
        <result property="putawayState"       column="putaway_state"      />
        <result property="ifOnline"           column="if_online"          />
        <result property="personLimit"        column="person_limit"       />
        <result property="oneDayLimit"        column="one_day_limit"      />
        <result property="imgUrl"             column="img_url"            />
        <result property="inventoryNum"       column="inventory_num"      />
        <result property="revision"           column="revision"           />
        <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="productCode"         column="product_code"        />
    </resultMap>

    <sql id="selectSupermarketProductVo">
        select id,supermarket_id,product_name,bar_code,sale_price,sales_mode,area_id,unit_id,material_id,pref_price,quality_type,quality_num,supply_certificate,product_remark,putaway_state,if_online,person_limit,one_day_limit,img_url,inventory_num,revision,create_by,create_time,update_by,update_time from supermarket_product
    </sql>

    <select id="selectSupermarketProductList" parameterType="com.bonus.canteen.core.supermarket.domain.SupermarketProduct" resultType="com.bonus.canteen.core.supermarket.domain.SupermarketProduct">
        select sp.product_id as productId,sp.supermarket_id as supermarketid,sp.product_name as productname,sp.bar_code as barcode,sp.sale_price as saleprice,sp.sales_mode as salesmode,
        sp.area_id as areaid,aa.area_name as areaname,sp.unit_id as unitid,sp.product_type as producttype,sp.pref_price as prefprice,sp.quality_type as qualitytype,sp.quality_num as qualitynum,
        sp.supply_certificate as supplycertificate,sp.product_remark as productremark,sp.putaway_state as putawaystate,sp.if_online as ifonline,sp.person_limit as personlimit,sp.one_day_limit as onedaylimit,sp.img_url as imgurl,sp.inventory_num as inventorynum,sp.revision,sp.create_by as createby,sp.create_time as createtime,sp.update_by as updateby,sp.update_time as updatetime,sp.product_code as productcode
        from supermarket_product sp
        left join alloc_area aa on sp.area_id=aa.area_id
        <where>  
            <if test="productType != null  and productType != ''"> and sp.product_type = #{productType}</if>
            <if test="areaId != null  and areaId != ''"> and sp.area_id = #{areaId}</if>
            <if test="productName != null  and productName != ''"> and sp.product_name like concat('%', #{productName}, '%')</if>
            <if test="barCode != null  and barCode != ''"> and sp.bar_code like concat('%', #{barCode}, '%')</if>
            <if test="productCode != null  and productCode != ''"> and sp.product_code like concat('%', #{productCode}, '%')</if>
        </where>
    </select>
        
    <insert id="insertSupermarketProduct" parameterType="com.bonus.canteen.core.supermarket.domain.SupermarketProduct">
        insert into supermarket_product
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="supermarketId    !=null and supermarketId     != '' "  >supermarket_id    , </if>
            <if test="productName      !=null and productName       != '' "  >product_name      , </if>
            <if test="barCode          !=null and barCode           != '' "  >bar_code          , </if>
            <if test="salePrice        !=null and salePrice         != '' "  >sale_price        , </if>
            <if test="salesMode        !=null and salesMode         != '' "  >sales_mode        , </if>
            <if test="areaId           !=null and areaId            != '' "  >area_id           , </if>
            <if test="unitId           !=null and unitId            != '' "  >unit_id           , </if>
            <if test="productType      !=null and productType       != '' " >product_type       , </if>
            <if test="prefPrice        !=null and prefPrice         != '' "  >pref_price        , </if>
            <if test="qualityType      !=null and qualityType       != '' "  >quality_type      , </if>
            <if test="qualityNum       !=null and qualityNum        != '' "  >quality_num       , </if>
            <if test="supplyCertificate!=null and supplyCertificate != '' "  >supply_certificate, </if>
            <if test="productRemark    !=null and productRemark     != '' "  >product_remark    , </if>
            <if test="putawayState     !=null and putawayState      != '' "  >putaway_state     , </if>
            <if test="ifOnline         !=null and ifOnline          != '' "  >if_online         , </if>
            <if test="personLimit      !=null and personLimit       != '' "  >person_limit      , </if>
            <if test="oneDayLimit      !=null and oneDayLimit       != '' "  >one_day_limit     , </if>
            <if test="imgUrl           !=null and imgUrl            != '' "  >img_url           , </if>
            <if test="inventoryNum     !=null and inventoryNum      != '' "  >inventory_num     , </if>
            <if test="revision         !=null and revision          != '' "  >revision          , </if>
            <if test="createBy         !=null and createBy          != '' "  >create_by         , </if>
            <if test="createTime       !=null and createTime        != '' "  >create_time       , </if>
            <if test="updateBy         !=null and updateBy          != '' "  >update_by         , </if>
            <if test="updateTime       !=null and updateTime        != '' "  >update_time       , </if>
            <if test="productCode      !=null and productCode       != '' "  >product_code      , </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="supermarketId    !=null and supermarketId     != '' "  >#{supermarketId    },</if>
            <if test="productName      !=null and productName       != '' "  >#{productName      },</if>
            <if test="barCode          !=null and barCode           != '' "  >#{barCode          },</if>
            <if test="salePrice        !=null and salePrice         != '' "  >#{salePrice        },</if>
            <if test="salesMode        !=null and salesMode         != '' "  >#{salesMode        },</if>
            <if test="areaId           !=null and areaId            != '' "  >#{areaId           },</if>
            <if test="unitId           !=null and unitId            != '' "  >#{unitId           },</if>
            <if test="productType      !=null and productType       != '' "  >#{productType      },</if>
            <if test="prefPrice        !=null and prefPrice         != '' "  >#{prefPrice        },</if>
            <if test="qualityType      !=null and qualityType       != '' "  >#{qualityType      },</if>
            <if test="qualityNum       !=null and qualityNum        != '' "  >#{qualityNum       },</if>
            <if test="supplyCertificate!=null and supplyCertificate != '' "  >#{supplyCertificate},</if>
            <if test="productRemark    !=null and productRemark     != '' "  >#{productRemark    },</if>
            <if test="putawayState     !=null and putawayState      != '' "  >#{putawayState     },</if>
            <if test="ifOnline         !=null and ifOnline          != '' "  >#{ifOnline         },</if>
            <if test="personLimit      !=null and personLimit       != '' "  >#{personLimit      },</if>
            <if test="oneDayLimit      !=null and oneDayLimit       != '' "  >#{oneDayLimit      },</if>
            <if test="imgUrl           !=null and imgUrl            != '' "  >#{imgUrl           },</if>
            <if test="inventoryNum     !=null and inventoryNum      != '' "  >#{inventoryNum     },</if>
            <if test="revision         !=null and revision          != '' "  >#{revision         },</if>
            <if test="createBy         !=null and createBy          != '' "  >#{createBy         },</if>
            <if test="createTime       !=null and createTime        != '' "  >#{createTime       },</if>
            <if test="updateBy         !=null and updateBy          != '' "  >#{updateBy         },</if>
            <if test="updateTime       !=null and updateTime        != '' "  >#{updateTime       },</if>
            <if test="productCode      !=null and productCode       != '' "  >#{productCode      },</if>
         </trim>
    </insert>

    <update id="updateSupermarketProduct" parameterType="com.bonus.canteen.core.supermarket.domain.SupermarketProduct">
        update supermarket_product
        <trim prefix="SET" suffixOverrides=",">
            <if test="supermarketId    !=null and supermarketId     != '' "  >supermarket_id    =#{supermarketId    },</if>
            <if test="productName      !=null and productName       != '' "  >product_name      =#{productName      },</if>
            <if test="barCode          !=null and barCode           != '' "  >bar_code          =#{barCode          },</if>
            <if test="salePrice        !=null and salePrice         != '' "  >sale_price        =#{salePrice        },</if>
            <if test="salesMode        !=null and salesMode         != '' "  >sales_mode        =#{salesMode        },</if>
            <if test="areaId           !=null and areaId            != '' "  >area_id           =#{areaId           },</if>
            <if test="unitId           !=null and unitId            != '' "  >unit_id           =#{unitId           },</if>
            <if test="productType      !=null and productType       != '' " >product_type      =#{productType     },</if>
            <if test="prefPrice        !=null and prefPrice         != '' "  >pref_price        =#{prefPrice        },</if>
            <if test="qualityType      !=null and qualityType       != '' "  >quality_type      =#{qualityType      },</if>
            <if test="qualityNum       !=null and qualityNum        != '' "  >quality_num       =#{qualityNum       },</if>
            <if test="supplyCertificate!=null and supplyCertificate != '' "  >supply_certificate=#{supplyCertificate},</if>
            <if test="productRemark    !=null and productRemark     != '' "  >product_remark    =#{productRemark    },</if>
            <if test="putawayState     !=null and putawayState      != '' "  >putaway_state     =#{putawayState     },</if>
            <if test="ifOnline         !=null and ifOnline          != '' "  >if_online         =#{ifOnline         },</if>
            <if test="personLimit      !=null and personLimit       != '' "  >person_limit      =#{personLimit      },</if>
            <if test="oneDayLimit      !=null and oneDayLimit       != '' "  >one_day_limit     =#{oneDayLimit      },</if>
            <if test="imgUrl           !=null and imgUrl            != '' "  >img_url           =#{imgUrl           },</if>
            <if test="inventoryNum     !=null and inventoryNum      != '' "  >inventory_num     =#{inventoryNum     },</if>
            <if test="revision         !=null and revision          != '' "  >revision          =#{revision         },</if>
            <if test="createBy         !=null and createBy          != '' "  >create_by         =#{createBy         },</if>
            <if test="createTime       !=null and createTime        != '' "  >create_time       =#{createTime       },</if>
            <if test="updateBy         !=null and updateBy          != '' "  >update_by         =#{updateBy         },</if>
            <if test="updateTime       !=null and updateTime        != '' "  >update_time       =#{updateTime       },</if>
            <if test="productCode      !=null and productCode       != '' "  >product_code      =#{productCode      },</if>
        </trim>
        where product_id = #{productId}
    </update>
    <update id="updateSupermarketProductState" parameterType="com.bonus.canteen.core.supermarket.domain.SupermarketProduct">
        update supermarket_product
        <trim prefix="SET" suffixOverrides=",">
            <if test="state      !=null and state       != '' "  >state      =#{state},</if>
        </trim>
        where product_id = #{productId}
    </update>
    <delete id="deleteSupermarketProductBySupermarketId" parameterType="Long">
        delete from supermarket_product where product_id = #{productId}
    </delete>

    <delete id="deleteSupermarketProductBySupermarketIds" parameterType="String">
        delete from supermarket_product where product_id in
        <foreach item="productId" collection="array" open="(" separator="," close=")">
            #{productId}
        </foreach>
    </delete>
</mapper>