package com.sunda.spmsorder.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sunda.spmsorder.entity.MaterialDeliveryBoards;
import com.sunda.spmsorder.entity.MaterialSpms;
import com.sunda.spmsorder.entity.vo.MaterialDeliveryBoardsQueryVO;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 物料表SPMS Mapper 接口
 * </p>
 *
 * @author Wayne
 * @since 2020-11-23
 */
@Mapper
@Component
public interface MaterialSpmsMapper extends BaseMapper<MaterialSpms> {
	
	@Select("<script>\n" +
            "SELECT\n" +
            "    SBND.MATERIAL_NO AS \"materialNo\",\n" +
            "    SBND.DELIVERY_NOTE_SAP AS \"deliveryNoteSap\",\n" +
            "    SBND.BOARDS AS \"boards\",\n" +
            "    SBND.MATERIAL_TYPE AS \"materialType\",\n" +
            "    SBND.CREATE_TIME AS \"createTime\",\n" +
            "    SBND.UPDATE_TIME AS \"updateTime\",\n" +
            "    SBND.CREATE_USER AS \"createUser\",\n" +
            "    SBND.UPDATE_USER AS \"updateUser\",\n" +
            "    MA.BASIC_UNIT AS \"unit\",\n" +
            "    MA.MATERIAL_ZH_DESC AS \"materialZhDesc\",\n" +
            "    MA.MATERIAL_EN_DESC AS \"materialEnDesc\"\n" +
            "FROM\n" +
            "    MATERIAL_DELIVERY_BOARDS SBND\n" +
            "    LEFT JOIN MATERIAL_SAP MA ON SBND.MATERIAL_NO = MA.MATERIAL_NO\n" +
            "WHERE 1 = 1 \n" +
            "    <when test='dto.materialNo !=null and dto.materialNo != \"\" '> AND (SBND.MATERIAL_NO = #{dto.materialNo} OR SBND.DELIVERY_NOTE_SAP = #{dto.materialNo})</when>\n" +
            "</script>")
	List<Map<String, Object>> selectMaterialBoardsList(IPage<Map<String, Object>> iPage, @Param("dto") MaterialDeliveryBoardsQueryVO dto);
	
	@Select("<script>\n" +
            "SELECT\n" +
            "    SBND.MATERIAL_NO AS \"materialNo\",\n" +
            "    SBND.DELIVERY_NOTE_SAP AS \"deliveryNoteSap\",\n" +
            "    SBND.BOARDS AS \"boards\",\n" +
            "    SBND.MATERIAL_TYPE AS \"materialType\",\n" +
            "    SBND.CREATE_TIME AS \"createTime\",\n" +
            "    SBND.UPDATE_TIME AS \"updateTime\",\n" +
            "    SBND.CREATE_USER AS \"createUser\",\n" +
            "    SBND.UPDATE_USER AS \"updateUser\" \n" +
            "FROM\n" +
            "    MATERIAL_DELIVERY_BOARDS SBND\n" +
            "WHERE 1 = 1 \n" +
            "    <when test='materialNo !=null and materialNo != \"\" '> AND SBND.MATERIAL_NO = #{materialNo}</when>\n" +
            "    <when test='deliveryNoteSap !=null and deliveryNoteSap != \"\"'> AND SBND.DELIVERY_NOTE_SAP = #{deliveryNoteSap}</when>\n" +
            "</script>")
	Map<String, Object> selectMaterialAndDelivery(@Param("materialNo") String materialNo, @Param("deliveryNoteSap") String deliveryNoteSap);
	
	@Insert("<script>\n" +
			"	INSERT INTO MATERIAL_DELIVERY_BOARDS ( \n" +
			"	\"MATERIAL_NO\" , \"DELIVERY_NOTE_SAP\", \"BOARDS\", \"MATERIAL_TYPE\", \n" +
			"	\"CREATE_TIME\", \"CREATE_USER\") VALUES ( \n" +
			"	#{materialNo}, #{deliveryNoteSap}, #{boards}, #{materialType}, #{createTime}, #{createUser}) \n" +
			"</script>")
	int insertMaterialAndDelivery(MaterialDeliveryBoards record);
	
	@Update("<script>\n" +
			"	UPDATE MATERIAL_DELIVERY_BOARDS SET BOARDS = #{boards}, MATERIAL_TYPE = #{materialType}, UPDATE_TIME = #{updateTime}, UPDATE_USER = #{updateUser} \n" +
			"	WHERE 1=1 \n" +
			"    <when test='materialNo !=null and materialNo != \"\" '> AND MATERIAL_NO = #{materialNo}</when>\n" +
            "    <when test='deliveryNoteSap !=null and deliveryNoteSap != \"\"'> AND DELIVERY_NOTE_SAP = #{deliveryNoteSap}</when>\n" +
			"</script>")
	int updateMaterialAndDelivery(MaterialDeliveryBoards record);
	
	@Delete("<script>\n" +
			"	DELETE FROM MATERIAL_SPMS \n" +
			"	WHERE MATERIAL_NO IN \n" +
			"	<foreach item='item' index='index' collection='list' open='(' separator=',' close=')'>\n" +
            "       #{item} \n" +
            "	</foreach>" +
			"</script>")
	int deleteListByMaterialNos(@Param("list") List<String> list);



}
