<?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.yeejoin.amos.boot.module.hygf.api.mapper.WorkOrderMapper">

   <select id="selectWorkOrderList"  resultType="com.yeejoin.amos.boot.module.hygf.api.entity.WorkOrder" >

     SELECT
	 hygf_work_order.*,
	 (SELECT count(*) from  hygf_work_order_power_station where hygf_work_order_power_station.work_order_id = hygf_work_order.sequence_nbr ) peasantHouseholdNum,

       (SELECT count(*) from  hygf_work_order_power_station where hygf_work_order_power_station.work_order_id = hygf_work_order.sequence_nbr

       and hygf_work_order_power_station.power_station_construction_status in ('待登记','未通过','已完工','待审核')

       ) peasantHouseholdSgNum,
       (
       SELECT
       GROUP_CONCAT( owners_name ORDER BY owners_name ASC SEPARATOR ', ' ) AS ownersName
       FROM
       hygf_work_order_power_station ps
       LEFT JOIN hygf_peasant_household ph ON ps.peasant_household_id = ph.sequence_nbr
       WHERE
       ps.work_order_id = hygf_work_order.sequence_nbr

       )  as ownersName
       FROM  hygf_work_order
       WHERE hygf_work_order.is_delete=0
       <if test="dto.workOrderNum!=null and dto.workOrderNum!=''">
           and hygf_work_order.work_order_num like concat(concat('%',#{dto.workOrderNum}),'%')
       </if>
       <if test="dto.projectRegionManager!=null and dto.projectRegionManager!=''">
           and hygf_work_order.project_region_manager like concat(concat('%',#{dto.projectRegionManager}),'%')
       </if>
       <if test="dto.workOrderStatus!=null and dto.workOrderStatus!=''">
           and hygf_work_order.work_order_status=#{dto.workOrderStatus}
       </if>
       <if test="dto.amosDealerId!=null and dto.amosDealerId!=''">
           and hygf_work_order.amos_dealer_id = #{dto.amosDealerId}
       </if>
       <if test="dto.regionCompanyId!=null and dto.regionCompanyId!=''">
           and hygf_work_order.region_company_id =#{dto.regionCompanyId}
       </if>
       <if test="dto.ownersName!=null and dto.ownersName!=''">
           and (
           SELECT
           GROUP_CONCAT( owners_name ORDER BY owners_name ASC SEPARATOR ', ' ) AS ownersName
           FROM
           hygf_work_order_power_station ps
           LEFT JOIN hygf_peasant_household ph ON ps.peasant_household_id = ph.sequence_nbr
           WHERE
           ps.work_order_id = hygf_work_order.sequence_nbr
           ) like concat ('%',#{dto.ownersName},'%')
       </if>
       ORDER BY
       hygf_work_order.rec_date DESC,

       FIELD(hygf_work_order.work_order_status, '待施工', '施工中', '待登记', '登记中', '待审核', '审核中', '未通过', '已完工')

   </select>


    <select id="getRealNameByUserId"  resultType="String" >
        select real_name from std_user_biz where amos_user_id =#{userId}
    </select>






    <select id="queryForwgpage"  resultType="com.yeejoin.amos.boot.module.hygf.api.dto.WorkOrderPage" >

        select
        hygf_peasant_household.sequence_nbr sequenceNbr,
        hygf_peasant_household.peasant_household_no peasantHouseholdNo,
        substring_index ( hygf_peasant_household.project_address_name, '/', 1 ) AS province,
        hygf_work_order.work_order_num workOrderNum,
        hygf_peasant_household.owners_name ownersName,
        hygf_work_order.project_region_manager projectRegionManager,
        hygf_work_order.construction_region_manager constructionRegionManager,
        hygf_power_station_engineering_info.completion_date completionDate ,
        hygf_peasant_household.project_address_name projectAddressName,
        hygf_work_order_power_station.power_station_construction_status powerStationConstructionStatus,
        hygf_work_order_power_station.sequence_nbr  workOrderPowerStationId,
        hygf_work_order_power_station.work_order_power_station_node  workOrderPowerStationNode,
        hygf_work_order_power_station.power_station_area_status powerStationAreaStatus ,
        hygf_work_order_power_station.power_station_design_status powerStationDesignStatus ,
        hygf_work_order_power_station.power_station_engineering_status powerStationEngineeringStatus,
        (select hygf_rectification_order.rectification_history  from  hygf_rectification_order
        where hygf_rectification_order.peasant_househole_id = hygf_work_order_power_station.peasant_household_id
        and hygf_rectification_order.rectification_status  = '待整改'  and hygf_rectification_order.rectification_source = hygf_work_order_power_station.work_order_power_station_node and hygf_rectification_order.rectification_source in ('area','engineering','design') )  as auditIdea,
        (select hygf_rectification_order.sequence_nbr  from  hygf_rectification_order
        where hygf_rectification_order.peasant_househole_id = hygf_work_order_power_station.peasant_household_id
        and hygf_rectification_order.rectification_status  = '已作废'  and hygf_rectification_order.rectification_source = hygf_work_order_power_station.work_order_power_station_node and hygf_rectification_order.rectification_source in ('area','engineering','design')order by rec_date desc limit 1) as rollbackOrderId,
        (select hygf_rectification_order.sequence_nbr  from  hygf_rectification_order
        where hygf_rectification_order.peasant_househole_id = hygf_work_order_power_station.peasant_household_id
          and hygf_rectification_order.rectification_status  in ('待整改','待提交','整改中','整改待审核','待审核') and hygf_rectification_order.rectification_source in ('area','engineering','design')) as rectificationNum
        from   hygf_work_order_power_station LEFT join hygf_work_order
        on hygf_work_order.sequence_nbr=hygf_work_order_power_station.work_order_id
        LEFT JOIN hygf_peasant_household on  hygf_peasant_household.sequence_nbr=hygf_work_order_power_station.peasant_household_id
        LEFT JOIN hygf_power_station_engineering_info on hygf_power_station_engineering_info.work_order_power_station_id=hygf_work_order_power_station.sequence_nbr
        <where>
        <if test="dto.workOrderId!=null and dto.workOrderId!=''">
            and hygf_work_order.sequence_nbr = #{dto.workOrderId}
        </if>
        <if test=' dto.type!=null and dto.type == "1"'>
            and  hygf_work_order_power_station.power_station_construction_status in ('待登记','未通过','已完工','待审核')
        </if>
        <if test=' dto.type !=null and dto.type == "2"'>
          and  hygf_work_order_power_station.power_station_construction_status IN ( '未通过', '已完工', '待审核', '待整改', '整改中','整改待审核' )
        </if>
        <if test=' dto.type !=null and dto.type == "3" and dto.workOrderPowerStationNodes  !=null'>
                and ( hygf_work_order_power_station.work_order_power_station_node  is not null
                or hygf_work_order_power_station.work_order_power_station_node = 'all'
                or hygf_work_order_power_station.power_station_area_status  is not null
                or hygf_work_order_power_station.power_station_design_status  is not null
                or hygf_work_order_power_station.power_station_engineering_status  is not null
            )
        </if>
        <if test="dto.projectAddressName!=null and dto.projectAddressName!=''">
            and hygf_peasant_household.project_address_name like concat(concat('%',#{dto.projectAddressName}),'%')
        </if>
        <if test="dto.peasantHouseholdNo!=null and dto.peasantHouseholdNo!=''">
            and hygf_peasant_household.peasant_household_no like concat(concat('%',#{dto.peasantHouseholdNo}),'%')
        </if>
        <if test="dto.ownersName!=null and dto.ownersName!=''">
            and hygf_peasant_household.owners_name like concat(concat('%',#{dto.ownersName}),'%')
        </if>
        <if test="dto.workOrderNum!=null and dto.workOrderNum!=''">
            and hygf_work_order.work_order_num like concat(concat('%',#{dto.workOrderNum}),'%')
        </if>
        <if test="dto.projectRegionManager!=null and dto.projectRegionManager!=''">
            and hygf_work_order.project_region_manager like concat(concat('%',#{dto.projectRegionManager}),'%')
        </if>
        <if test="dto.powerStationConstructionStatus!=null and dto.powerStationConstructionStatus!=''">
            and hygf_work_order_power_station.power_station_construction_status=#{dto.powerStationConstructionStatus}
        </if>
        <if test="dto.amosDealerId!=null and dto.amosDealerId!=''">
            and hygf_work_order.amos_dealer_id = #{dto.amosDealerId}
        </if>
        <if test="dto.regionCompanyId!=null and dto.regionCompanyId!=''">
            and hygf_work_order.region_company_id =#{dto.regionCompanyId}
        </if>
        <if test="dto.province != null and dto.province != ''">
            and hygf_peasant_household.project_address like concat(concat('%',#{dto.province}),'%')
        </if>
        </where>
        ORDER BY
        hygf_work_order_power_station.sequence_nbr DESC
    </select>

</mapper>
