Files
zhyc-sheep/zhyc-module/src/main/resources/mapper/mating_plan/ScBreedPlanGenerateMapper.xml

208 lines
9.8 KiB
XML
Raw Normal View History

<?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.zhyc.module.produce.mating_plan.mapper.ScBreedPlanGenerateMapper">
<resultMap type="ScBreedPlanGenerate" id="ScBreedPlanGenerateResult">
<result property="id" column="id" />
<result property="planName" column="plan_name" />
<result property="planType" column="plan_type" />
<result property="planDate" column="plan_date" />
<result property="totalEweCount" column="total_ewe_count" />
<result property="totalRamCount" column="total_ram_count" />
<result property="breedRatio" column="breed_ratio" />
<result property="status" column="status" />
<result property="approver" column="approver" />
<result property="approveTime" column="approve_time" />
<result property="approveRemark" column="approve_remark" />
<result property="createBy" column="create_by" />
<result property="createTime" column="create_time" />
<result property="updateBy" column="update_by" />
<result property="updateTime" column="update_time" />
</resultMap>
<sql id="selectScBreedPlanGenerateVo">
select id, plan_name, plan_type, plan_date, total_ewe_count, total_ram_count,
breed_ratio, status, approver, approve_time, approve_remark,
create_by, create_time, update_by, update_time
from sc_breed_plan_generate
</sql>
<select id="selectScBreedPlanGenerateList" parameterType="ScBreedPlanGenerate" resultMap="ScBreedPlanGenerateResult">
<include refid="selectScBreedPlanGenerateVo"/>
<where>
<if test="planName != null and planName != ''"> and plan_name like concat('%', #{planName}, '%')</if>
<if test="planType != null"> and plan_type = #{planType}</if>
<if test="planDate != null"> and plan_date = #{planDate}</if>
<if test="status != null"> and status = #{status}</if>
</where>
order by create_time desc
</select>
<select id="selectScBreedPlanGenerateById" parameterType="Long" resultMap="ScBreedPlanGenerateResult">
<include refid="selectScBreedPlanGenerateVo"/>
where id = #{id}
</select>
<!-- 筛选符合条件的母羊 -->
<select id="selectEligibleEwe" resultType="Map">
select
sf.id,
sf.bs_manage_tags,
sf.variety,
sf.name as sheep_type,
sf.gender,
sf.month_age,
sf.current_weight,
sf.post_lambing_day,
sf.breed
from sheep_file sf
where sf.gender = 1
and sf.is_delete = 0
and (sf.status_id = 1 or sf.status_id is null)
and (
-- 青年羊或超龄羊的配种条件
(sf.name in ('青年羊', '超龄羊') and (
(sf.variety = '湖羊' and sf.month_age >= 7.5 and sf.current_weight >= 33) or
(sf.variety = '东佛里生' and sf.month_age >= 9 and sf.current_weight >= 50) or
(sf.variety not in ('湖羊', '东佛里生') and sf.month_age >= 9 and sf.current_weight >= 50)
))
or
-- 泌乳羊或种母羊的配种条件
(sf.name in ('泌乳羊', '种母羊') and sf.post_lambing_day > 45 and sf.current_weight > 0)
)
order by sf.variety, sf.month_age desc
</select>
<!-- 筛选符合条件的公羊 -->
<select id="selectEligibleRam" resultType="Map">
select
sf.id,
sf.manage_tags as bs_manage_tags,
bv.variety as variety,
bst.name as sheep_type,
sf.gender,
TIMESTAMPDIFF(MONTH, sf.birthday, NOW()) as month_age,
sf.current_weight,
bbs.breed as breed
from bas_sheep sf
left join bas_sheep_variety bv on sf.variety_id = bv.id
left join bas_sheep_type bst on sf.type_id = bst.id
left join bas_breed_status bbs on sf.breed_status_id = bbs.id
where sf.gender = 2
and sf.is_delete = 0
and sf.status_id = 1
and (
-- 青年羊或超龄羊的参配条件
(bst.name in ('青年羊', '超龄羊') and (
(bv.variety = '湖羊' and TIMESTAMPDIFF(MONTH, sf.birthday, NOW()) >= 7.5 and sf.current_weight >= 33) or
(bv.variety = '东佛里生' and TIMESTAMPDIFF(MONTH, sf.birthday, NOW()) >= 9 and sf.current_weight >= 50) or
(bv.variety not in ('湖羊', '东佛里生') and TIMESTAMPDIFF(MONTH, sf.birthday, NOW()) >= 9 and sf.current_weight >= 50)
))
or
-- 其他类型公羊
bst.name not in ('青年羊', '超龄羊')
)
order by bv.variety, TIMESTAMPDIFF(MONTH, sf.birthday, NOW()) desc
</select>
<insert id="insertScBreedPlanGenerate" parameterType="ScBreedPlanGenerate" useGeneratedKeys="true" keyProperty="id">
insert into sc_breed_plan_generate
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="planName != null">plan_name,</if>
<if test="planType != null">plan_type,</if>
<if test="planDate != null">plan_date,</if>
<if test="totalEweCount != null">total_ewe_count,</if>
<if test="totalRamCount != null">total_ram_count,</if>
<if test="breedRatio != null">breed_ratio,</if>
<if test="status != null">status,</if>
<if test="approver != null">approver,</if>
<if test="approveTime != null">approve_time,</if>
<if test="approveRemark != null">approve_remark,</if>
<if test="createBy != null">create_by,</if>
<if test="createTime != null">create_time,</if>
<if test="updateBy != null">update_by,</if>
<if test="updateTime != null">update_time,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="planName != null">#{planName},</if>
<if test="planType != null">#{planType},</if>
<if test="planDate != null">#{planDate},</if>
<if test="totalEweCount != null">#{totalEweCount},</if>
<if test="totalRamCount != null">#{totalRamCount},</if>
<if test="breedRatio != null">#{breedRatio},</if>
<if test="status != null">#{status},</if>
<if test="approver != null">#{approver},</if>
<if test="approveTime != null">#{approveTime},</if>
<if test="approveRemark != null">#{approveRemark},</if>
<if test="createBy != null">#{createBy},</if>
<if test="createTime != null">#{createTime},</if>
<if test="updateBy != null">#{updateBy},</if>
<if test="updateTime != null">#{updateTime},</if>
</trim>
</insert>
<!-- 插入临时配种计划 -->
<insert id="insertTempBreedPlan">
insert into sc_breed_plan_temp (plan_generate_id, ram_id, ewe_id, breed_type, create_time)
values (#{planGenerateId}, #{breedPlan.ramId}, #{breedPlan.eweId}, #{breedPlan.breedType}, now())
</insert>
<update id="updateScBreedPlanGenerate" parameterType="ScBreedPlanGenerate">
update sc_breed_plan_generate
<trim prefix="SET" suffixOverrides=",">
<if test="planName != null">plan_name = #{planName},</if>
<if test="planType != null">plan_type = #{planType},</if>
<if test="planDate != null">plan_date = #{planDate},</if>
<if test="totalEweCount != null">total_ewe_count = #{totalEweCount},</if>
<if test="totalRamCount != null">total_ram_count = #{totalRamCount},</if>
<if test="breedRatio != null">breed_ratio = #{breedRatio},</if>
<if test="status != null">status = #{status},</if>
<if test="approver != null">approver = #{approver},</if>
<if test="approveTime != null">approve_time = #{approveTime},</if>
<if test="approveRemark != null">approve_remark = #{approveRemark},</if>
<if test="updateBy != null">update_by = #{updateBy},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
</trim>
where id = #{id}
</update>
<!-- 将临时配种计划转为正式配种计划 -->
<insert id="transferTempToFormal">
insert into sc_breed_plan (ram_id, ewe_id, breed_type)
select ram_id, ewe_id, breed_type
from sc_breed_plan_temp
where plan_generate_id = #{planGenerateId}
</insert>
<!-- 获取配种计划详情 -->
<select id="selectBreedPlanDetails" parameterType="Long" resultType="Map">
select
temp.id,
temp.ram_id,
temp.ewe_id,
temp.breed_type,
ram.bs_manage_tags as ram_manage_tags,
ram.variety as ram_variety,
ewe.bs_manage_tags as ewe_manage_tags,
ewe.variety as ewe_variety,
ewe.current_weight as ewe_weight
from sc_breed_plan_temp temp
left join sheep_file ram on temp.ram_id = ram.id
left join sheep_file ewe on temp.ewe_id = ewe.id
where temp.plan_generate_id = #{planGenerateId}
order by temp.ram_id, temp.ewe_id
</select>
<delete id="deleteScBreedPlanGenerateById" parameterType="Long">
delete from sc_breed_plan_generate where id = #{id}
</delete>
<delete id="deleteScBreedPlanGenerateByIds" parameterType="String">
delete from sc_breed_plan_generate where id in
<foreach item="id" collection="array" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
</mapper>