<?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.jeeplus.modules.casematerials.dao.CaseMaterialsDao">
    
	<sql id="caseMaterialsColumns">
		a.id AS "id",
		a.create_by AS "createBy.id",
		a.create_date AS "createDate",
		a.update_by AS "updateBy.id",
		a.update_date AS "updateDate",
		a.remarks AS "remarks",
		a.del_flag AS "delFlag",
		a.name AS "name",
		a.brand AS "brand",
		a.specification AS "specification",
		a.parameter_performance AS "pp",
		a.calculate_unit AS "calculateUnit",
		a.price AS "price",
		a.count AS "count",
		a.supplier AS "supplier",
		a.linkedman AS "linkedman",
		a.mobile AS "mobile",
		a.area_id AS "area.id",
		area.name AS "area.name",
		a.taxPrice AS "taxPrice",
		a.useDate AS "useDate",
		a.source AS "source",
		a.tax AS "tax",
		a.material AS "material",
		a.case_id AS "caseId"
	</sql>
	
	<sql id="caseMaterialsJoins">
		LEFT JOIN sys_area area ON area.id = a.area_id
	</sql>
	
    
	<select id="get" resultType="CaseMaterials" >
		SELECT 
			<include refid="caseMaterialsColumns"/>
		FROM case_materials a
		<include refid="caseMaterialsJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="CaseMaterials" >
		SELECT 
			<include refid="caseMaterialsColumns"/>
		FROM case_materials a
		<include refid="caseMaterialsJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
			<if test="caseId!= null and caseId!= ''">
				AND a.case_id = #{caseId}
			</if>
			<if test="name != null and name != ''">
				AND a.name LIKE 
					<if test="dbName == 'oracle'">'%'||#{name}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{name}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{name},'%')</if>
			</if>
			<if test="brand != null and brand != ''">
				AND a.brand LIKE 
					<if test="dbName == 'oracle'">'%'||#{brand}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{brand}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{brand},'%')</if>
			</if>
			<if test="supplier != null and supplier != ''">
				AND a.supplier LIKE 
					<if test="dbName == 'oracle'">'%'||#{supplier}||'%'</if>
					<if test="dbName == 'mssql'">'%'+#{supplier}+'%'</if>
					<if test="dbName == 'mysql'">concat('%',#{supplier},'%')</if>
			</if>
		</where>
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<select id="findAllList" resultType="CaseMaterials" >
		SELECT 
			<include refid="caseMaterialsColumns"/>
		FROM case_materials a
		<include refid="caseMaterialsJoins"/>
		<where>
			a.del_flag = #{DEL_FLAG_NORMAL}
		</where>		
		<choose>
			<when test="page !=null and page.orderBy != null and page.orderBy != ''">
				ORDER BY ${page.orderBy}
			</when>
			<otherwise>
				ORDER BY a.update_date DESC
			</otherwise>
		</choose>
	</select>
	
	<insert id="insert">
		INSERT INTO case_materials(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			remarks,
			del_flag,
			name,
			brand,
			specification,
			parameter_performance,
			calculate_unit,
			price,
			count,
			supplier,
			linkedman,
			mobile,
			area_id,
			taxPrice,
			useDate,
			source,
			tax,
			material,
			case_id
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{remarks},
			#{delFlag},
			#{name},
			#{brand},
			#{specification},
			#{pp},
			#{calculateUnit},
			#{price},
			#{count},
			#{supplier},
			#{linkedman},
			#{mobile},
			#{area.id},
			#{taxPrice},
			#{useDate},
			#{source},
			#{tax},
			#{material},
			#{caseId}
		)
	</insert>
	
	<update id="update">
		UPDATE case_materials SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			remarks = #{remarks},
			name = #{name},
			brand = #{brand},
			specification = #{specification},
			parameter_performance = #{pp},
			calculate_unit = #{calculateUnit},
			price = #{price},
			count = #{count},
			supplier = #{supplier},
			linkedman = #{linkedman},
			mobile = #{mobile},
			area_id = #{area.id},
			taxPrice = #{taxPrice},
			useDate = #{useDate},
			source = #{source},
			tax = #{tax},
			material = #{material},
			case_id = #{caseId}
		WHERE id = #{id}
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM case_materials
		WHERE id = #{id}
	</update>

	<update id="deleteCase">
		DELETE FROM case_materials
		WHERE case_id = #{caseId}
	</update>
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE case_materials SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="CaseMaterials" statementType="STATEMENT">
		select * FROM case_materials  where ${propertyName} = '${value}'
	</select>
	
</mapper>