<?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.dayou.mapper.PersonalProductionMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dayou.entity.PersonalProduction">
        <result column="id" property="id" />
        <result column="deleted" property="deleted" />
        <result column="created" property="created" />
        <result column="modified" property="modified" />
        <result column="target_id" property="targetId" />
        <result column="production" property="production" />
        <result column="qr_code" property="qrCode" />
        <result column="repertory_state" property="repertoryState" />
        <result column="repertory_in_time" property="repertoryInTime" />
        <result column="repertory_out_time" property="repertoryOutTime" />
        <result column="check_state" property="checkState" />
        <result column="file_path" property="filePath" />
        <result column="user_id" property="userId" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        deleted,
        created,
        modified,
        target_id, production, qr_code, repertory_state, repertory_in_time, repertory_out_time, check_state, file_path, user_id
    </sql>

    <select id="getProductions" parameterType="java.lang.Long" resultType="com.dayou.dto.ProductionDTO">
        SELECT
            pp.*,
            p.order_id as productionNo,
            pt.location AS NAME,
            pt.acreage AS evaluateAcreage,
            pt.price AS evaluatePrice,
            pt.amount AS evaluateAmount,
            trim(
                    BOTH '"'
                    FROM
                    JSON_EXTRACT( pt.selling_ability, '$.createDateD' )) AS createDate,
            trim(
                    BOTH '"'
                    FROM
                    JSON_EXTRACT( pt.selling_ability, '$.sceneDateD' )) AS sceneDate,
            trim(
                    BOTH '"'
                    FROM
                    JSON_EXTRACT( pt.selling_ability, '$.limitDate' )) AS limitDate
        FROM
            (
                SELECT
                    id,
                    target_id,
                    production,
                    repertory_state,
                    repertory_in_time,
                    repertory_out_time
                FROM
                    `personal_production`
                WHERE
                        target_id = ( SELECT id FROM personal_target WHERE personal_id = #{id} AND deleted = 0 )) pp
                LEFT JOIN personal_target pt ON pt.id = pp.target_id
                left join personal p on pt.personal_id = p.id
        WHERE
            pt.deleted = 0 and
            p.deleted = 0
    </select>

    <select id="getProductionAfter2025" resultType="com.dayou.dto.PersonalProductionDTO">
        SELECT
            pp.id,
            pp.production,
            p.id AS pid,
            p.order_id
        FROM
            ( SELECT id, order_id FROM personal WHERE created &gt;= '2025-01-01 00:00:00' AND deleted = 0 ) p
                INNER JOIN personal_target pt ON pt.personal_id = p.id
                INNER JOIN personal_production pp ON pp.target_id = pt.id
        WHERE
            pt.deleted = 0
          AND pp.deleted = 0
    </select>
</mapper>
