package com.prolog.eis.dao.shelves;

import com.prolog.eis.dto.shelves.LocationLimitedDto;
import com.prolog.eis.model.shelves.LimitedGoodsAllocationInfo;
import com.prolog.framework.dao.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface LimitedGoodsAllocationMapper extends BaseMapper<LimitedGoodsAllocationInfo> {

    @Select("<script>\r\n" +
            "select \n" +
            "            lgai.id, \n" +
            "            lgai.goods_owner_id   goodsOwnerId, \n" +
            "            lgai.goods_owner_name  goodsOwnerName, \n" +
            "            lgai.goods_barcode goodsBarcode  , \n" +
            "            lgai.goods_name goodsName , \n" +
            "            lgai.goods_type  goodsType , \n" +
            "            lgai.limited_area limitedArea, \n "+
            "            lgai.goods_big_type goodsBigType , \n" +
            "            lgai.create_by createBy, \n" +
            "            lgai.create_time createTime, \n" +
            "            lgai.update_by updateBy, \n" +
            "            lgai.update_time  updateTime, \n" +
            "            lgai.notes  \n" +
            " from limited_goods_allocation_info lgai "+
            "where 1=1\r\n" +
            "<if test=\"code!='' and code!=null\">" +
            "and lgai.goods_barcode like concat('%',#{dto.goodsBarcode},'%')\r\n" +
            "</if>\r\n" +
            "<if test=\"name!='' and name!=null\">" +
            "and lgai.goods_name like concat('%',#{dto.goodsName},'%')\r\n" +
            "</if>\r\n" +
            "</script>")
    List<LimitedGoodsAllocationInfo> getLimitedGoodsAllocationInfo(@Param("dto") LocationLimitedDto dto);

    @Select("select limited_area from limited_goods_allocation_info where id = #{id}")
    String getLimitedGoodsAllocationDetails(@Param("id") String id);

    @Insert("<script>insert into limited_goods_allocation_info ( \r\n" +
            "       goods_owner_id,\r\n" +
            "       goods_owner_name,\r\n" +
            "       goods_barcode,\r\n" +
            "       goods_name,\r\n" +
            "       goods_type,\r\n" +
            "       goods_big_type,\r\n" +
            "       limited_area,\r\n" +
            "       create_by,\r\n" +
            "       create_time,\r\n" +
            "       update_by,\r\n" +
            "       update_time,\r\n" +
            "       notes) \r\n" +
            "       values \r\n" +
            "<foreach collection=\"list\" item=\"item\" index=\"index\" open=\"\" close=\"\" separator=\",\">\r\n" +
            "( \r\n" +
            "       #{item.goodsOwnerId},\r\n" +
            "       #{item.goodsOwnerName},\r\n" +
            "       #{item.goodsBarcode},\r\n" +
            "       #{item.goodsName},\r\n" +
            "       #{item.goodsType},\r\n" +
            "       #{item.goodsBigType},\r\n" +
            "       #{item.limitedArea},\r\n" +
            "       #{item.createBy},\r\n" +
            "       #{item.createTime},\r\n" +
            "       #{item.updateBy},\r\n" +
            "       #{item.updateTime},\r\n" +
            "       #{item.notes}\r\n" +
            ")\r\n" +
            "</foreach></script>")
    void insertLimitedGoodsAllocationInfo(@Param("list") List<LimitedGoodsAllocationInfo> goods_allocation_info);

    @Insert("<script>insert into limited_goods_allocation_info ( \r\n" +
            "       goods_owner_id,\r\n" +
            "       goods_owner_name,\r\n" +
            "       goods_barcode,\r\n" +
            "       goods_name,\r\n" +
            "       goods_type,\r\n" +
            "       goods_big_type,\r\n" +
            "       create_by,\r\n" +
            "       create_time,\r\n" +
            "       update_by,\r\n" +
            "       update_time,\r\n" +
            "       notes) \r\n" +
            "       values \r\n" +
            "<foreach collection=\"list\" item=\"item\" index=\"index\" open=\"\" close=\"\" separator=\",\">\r\n" +
            "( \r\n" +
            "       #{item.goodsOwnerId},\r\n" +
            "       #{item.goodsOwnerName},\r\n" +
            "       #{item.goodsBarcode},\r\n" +
            "       #{item.goodsName},\r\n" +
            "       #{item.goodsType},\r\n" +
            "       #{item.goodsBigType},\r\n" +
            "       #{item.createBy},\r\n" +
            "       #{item.createTime},\r\n" +
            "       #{item.updateBy},\r\n" +
            "       #{item.updateTime},\r\n" +
            "       #{item.notes}\r\n" +
            ")\r\n" +
            "</foreach></script>")
    void insertLimitedGoodsAllocationInfoNoDetails(@Param("list") List<LimitedGoodsAllocationInfo> goods_allocation_info);


    @Update("<script>\r\n" +
            "<foreach collection=\"list\" item=\"item\" index=\"index\" open=\"\" close=\"\" separator=\";\">\r\n" +
            "update \r\n" +
            "limited_goods_allocation_info\r\n" +
            "set \r\n" +
            "<if test=\"item.goodsOwnerId!='' and item.goodsOwnerId!=null\">" +
            "   goods_owner_id = #{item.goodsOwnerId},\r\n" +
            "</if>\r\n" +
            "<if test=\"item.goodsOwnerName!='' and item.goodsOwnerName!=null\">" +
            "	goods_owner_name = #{item.goodsOwnerName},\r\n" +
            "</if>\r\n" +
            "<if test=\"item.goodsBarcode!='' and item.goodsBarcode!=null\">" +
            "	goods_barcode = #{item.goodsBarcode},\r\n" +
            "</if>\r\n" +
            "<if test=\"item.goodsName!='' and item.goodsName!=null\">" +
            "	goods_name = #{item.goodsName},\r\n" +
            "</if>\r\n" +
            "<if test=\"item.goodsType!='' and item.goodsType!=null\">" +
            "	goods_type = #{item.goodsType},\r\n" +
            "</if>\r\n" +
            "<if test=\"item.goodsBigType!='' and item.goodsBigType!=null\">" +
            "	goods_big_type = #{item.goodsBigType},\r\n" +
            "</if>\r\n" +
            "<if test=\"item.limitedArea!='' and item.limitedArea!=null\">" +
            "	limited_area = #{item.limitedArea},\r\n" +
            "</if>\r\n" +
            "<if test=\"item.notes!='' and item.notes!=null\">" +
            "	notes = #{item.notes},\r\n" +
            "</if>\r\n" +
            "   update_time = #{item.updateTime},\r\n" +
            "   update_by = #{item.updateBy}\r\n" +
            " where id = #{item.id}\r\n" +
            "</foreach>\r\n" +
            "</script>")
    void updateLimitedGoodsAllocationInfo(@Param("list") List<LimitedGoodsAllocationInfo> goods_allocation_info);

    @Update("<script>\r\n" +
            "<foreach collection=\"list\" item=\"item\" index=\"index\" open=\"\" close=\"\" separator=\";\">\r\n" +
            "update \r\n" +
            "limited_goods_allocation_info\r\n" +
            "set \r\n" +
            "<if test=\"item.limitedArea!='' and item.limitedArea!=null\">" +
            "	limited_area = #{item.limitedArea}\r\n" +
            "</if>\r\n" +
            " where id = #{item.id}\r\n" +
            "</foreach>\r\n" +
            "</script>")
    void updateLimitedGoodsAllocationDetails(@Param("list") List<LimitedGoodsAllocationInfo> goods_allocation_info);

    @Delete("delete from limited_goods_allocation_info where id=#{id}")
    void deleteLimitedGoodsAllocationInfo(@Param("id") String id);

    @Update( "update \r\n" +
            "limited_goods_allocation_info\r\n" +
            "set \r\n" +
            "   limited_area = #{detail}\r\n" +
            " where id = #{id}\r\n" )
    void updateLimitedGoodsAllocationDetail(@Param("id")Integer id,@Param("detail") String detail);
}
