<?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.serialnum.dao.SerialNumTplDao">
    
	<sql id="serialNumTplColumns">
		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.company_id AS "company.id",
		a.biz_code AS "bizCode",
		a.year_built AS "yearBuilt",
		a.serial_num AS "serialNum",
		a.serial_tpl AS "serialTpl",
		a.serial_num_len AS "serialNumLen",
		a.tpl_name AS "tplName",
		a.serial_tpl_ex AS "serialTplEx",
		s.name AS "company.name",
		s.simple_name AS "company.simpleName",
		p.simple_name AS "company.parent.simpleName"
	</sql>
	
	<sql id="serialNumTplJoins">
		LEFT JOIN sys_office s on a.company_id = s.id
		LEFT JOIN sys_office p ON p.id = s.parent_id
	</sql>
	
    
	<select id="get" resultType="SerialNumTpl" >
		SELECT 
			<include refid="serialNumTplColumns"/>
		FROM sys_serialnum_tpl a
		<include refid="serialNumTplJoins"/>
		WHERE a.id = #{id}
	</select>
	
	<select id="findList" resultType="SerialNumTpl" >
		SELECT 
			<include refid="serialNumTplColumns"/>
		FROM sys_serialnum_tpl a
		<include refid="serialNumTplJoins"/>
		<where>
            <if test="company != null and company.id != null and company.id !=''">
                AND (a.company_id = #{company.id})
            </if>
			<if test="company != null and company.name != null and company.name !=''">
				AND s.name LIKE  concat('%',#{company.name},'%')
			</if>
			<if test="tplName != null and tplName != ''">
				AND a.tpl_name LIKE  concat('%',#{tplName},'%')
			</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="SerialNumTpl" >
		SELECT 
			<include refid="serialNumTplColumns"/>
		FROM sys_serialnum_tpl a
		<include refid="serialNumTplJoins"/>
		<where>
			
		</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 sys_serialnum_tpl(
			id,
			create_by,
			create_date,
			update_by,
			update_date,
			company_id,
			biz_code,
			year_built,
			serial_num,
			serial_tpl,
			serial_num_len,
			tpl_name,
			serial_tpl_ex
		) VALUES (
			#{id},
			#{createBy.id},
			#{createDate},
			#{updateBy.id},
			#{updateDate},
			#{company.id},
			#{bizCode},
			#{yearBuilt},
			#{serialNum},
			#{serialTpl},
			#{serialNumLen},
			#{tplName},
			#{serialTplEx}
		)
	</insert>
	
	<update id="update">
		UPDATE sys_serialnum_tpl SET 	
			update_by = #{updateBy.id},
			update_date = #{updateDate},
			company_id = #{company.id},
			biz_code = #{bizCode},
			year_built = #{yearBuilt},
			serial_num = #{serialNum},
			serial_tpl = #{serialTpl},
			serial_num_len = #{serialNumLen},
			tpl_name = #{tplName},
			serial_tpl_ex = #{serialTplEx}
		WHERE id = #{id}
		<if test="serialNum!=null and oldSerialNum != null">
            AND serial_num = #{oldSerialNum}
        </if>
	</update>
	
	
	<!--物理删除-->
	<update id="delete">
		DELETE FROM sys_serialnum_tpl
		WHERE id = #{id}
	</update>
	
	<!--逻辑删除-->
	<update id="deleteByLogic">
		UPDATE sys_serialnum_tpl SET 
			del_flag = #{DEL_FLAG_DELETE}
		WHERE id = #{id}
	</update>
	
	
	<!-- 根据实体名称和字段名称和字段值获取唯一记录 -->
	<select id="findUniqueByProperty" resultType="SerialNumTpl" statementType="STATEMENT">
		select * FROM sys_serialnum_tpl  where ${propertyName} = '${value}'
	</select>

	<select id="queryByComAndBizCode" resultType="SerialNumTpl">
		select	*
		 from sys_serialnum_tpl a
			where a.company_id = #{companyId} and a.biz_code = #{bizCode}
	</select>

	<update id="updateSerialNum">
		UPDATE sys_serialnum_tpl
		SET serial_num = serial_num +1
		WHERE id = #{id}
	</update>
	<update id="resetSerialNum">
		UPDATE sys_serialnum_tpl
		SET serial_num = 0 , year_built = #{newYear}
		WHERE id = #{id} AND  year_built = #{yearBuilt}
	</update>
</mapper>