/*
 * Author: wangjian
 * 生成mapper
 */
package com.tofly.gpsboot.mapper.maintain;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tofly.pojo.maintain.Repairorder;
import com.tofly.pojo.maintain.Repairtype;
import com.tofly.pojo.plan.Plan;
import com.tofly.pojo.statement.vo.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

/**
 * 维修类型信息表
 *
 * @author admin
 * @date Tue Apr 06 00:00:00 CST 2021
 */
@Mapper
public interface RepairtypeMapper extends BaseMapper<Repairtype> {
    /**
     * 获取维修类型统计
     * @param page
     * @param queryWrapper
     * @return
     */
    @Select("SELECT  " +
            "a.id as orderId, " +
            "a.ORDER_CODE as orderCode, " +
            "a.ORDER_SOURCE as orderSource, " +
            "( SELECT CNAME " +
            "        FROM " +
            "            TF_YWPN_SYSCHILDCODE_D " +
            "        WHERE " +
            "            PCID =555 " +
            "          AND " +
            "            CCODE = to_char(a.ORDER_SOURCE)) as orderSourceName, " +
            "a.TYPE_ID as typeId, " +
            "( SELECT NAME " +
            "        FROM " +
            "            TF_YWPN_REPAIRTYPE_D " +
            "        WHERE " +
            "            ID = a.TYPE_ID ) as typeName, " +
            "a.DISPATCHING_ID as dispatchingId, " +
            "( select REAL_NAME  " +
            "        from TF_SMPN_USER_X " +
            "        where id= a.DISPATCHING_ID ) as dispatchingName, " +
            "a.NOTES, " +
            "c.GDBH, " +
            "c.KHBH, " +
            "c.khxm, " +
            "c.khdz, " +
            "d.PHONE as mobile, " +
            "d.RESP_USER as disposeUserId, " +
            "( select REAL_NAME  " +
            "        from TF_SMPN_USER_X " +
            "        where id= d.RESP_USER ) as disposeUserName, " +
            "(select NAME as deptName " +
            "        from TF_SMPN_DEPARTMENT_X " +
            "        where id = " +
            "              (select DEPARTMENT_ID " +
            "               from TF_SMPN_USER_X " +
            "               where id = d.RESP_USER )) as disposeDeptName, " +
            "d.CREATE_TIME as dispatchingTime, " +
            "e.CREATE_TIME as disposeTime, " +
            "e.ADDRESS as disposeAddress, " +
            "e.REGION_NAME as regionName, " +
            "e.NOTES as disposeNote," +
            "e.BORE " +
            "FROM " +
            "TF_YWPN_REPAIRORDER_W a LEFT JOIN  " +
            "(SELECT " +
            "T1.id as orderId,t2.GDBH " +
            "FROM " +
            "TF_YWPN_REPAIRORDER_W t1 " +
            "LEFT JOIN TF_YWPN_GDCIRCULATION_W t2 ON t2.OTHER_BH = t1. ID) b ON a.id = b.orderId " +
            "LEFT JOIN TF_YWPN_GDINFO_W c on b.GDBH = c.GDBH  " +
            "LEFT JOIN TF_YWPN_REPAIRUSER_W d on a.id = d.REPAIR_ID " +
            "LEFT JOIN TF_YWPN_REPAIRRECORD_W e ON d.ID = e.REPAIR_USERID " +
            "${ew.customSqlSegment} ")
    Page<MaintenanceLogLedgerVo> getPageMaintenanceTypeStatisticsInfo(Page<MaintenanceTypeStatisticsVo> page, @Param(Constants.WRAPPER) QueryWrapper<Repairorder> queryWrapper);


    @Select("SELECT  " +
            "a.id as orderId, " +
            "a.ORDER_CODE as orderCode, " +
            "a.TYPE_ID as typeId, " +
            "( SELECT NAME " +
            "        FROM " +
            "            TF_YWPN_REPAIRTYPE_D " +
            "        WHERE " +
            "            ID = a.TYPE_ID ) as typeName, " +
            "a.DISPATCHING_ID as dispatchingId, " +
            "( select REAL_NAME  " +
            "        from TF_SMPN_USER_X " +
            "        where id= a.DISPATCHING_ID ) as dispatchingName, " +
            "a.NOTES, " +
            "d.PHONE as mobile, " +
            "d.RESP_USER as disposeUserId, " +
            " " +
            "( select REAL_NAME  " +
            "        from TF_SMPN_USER_X " +
            "        where id= d.RESP_USER ) as disposeUserName, " +
            "(select NAME as deptName " +
            "        from TF_SMPN_DEPARTMENT_X " +
            "        where id = " +
            "              (select DEPARTMENT_ID " +
            "               from TF_SMPN_USER_X " +
            "               where id = d.RESP_USER )) as disposeDeptName, " +
            "d.CREATE_TIME as dispatchingTime, " +
            "e.REGION_NAME as regionName, " +
            "e.CREATE_TIME as disposeTime, " +
            "e.FACILITIES_TYPE as facilitiesType," +
            "e.FACILITIES_CALIBER as facilitiesCaliber," +
            "e.CODE as facilitiesCode," +
            "e.VALVE_LOCATION as disposeAddress " +
            "FROM " +
            "TF_YWPN_REPAIRORDER_W a   " +
            "LEFT JOIN TF_YWPN_REPAIRUSER_W d on a.id = d.REPAIR_ID " +
            "LEFT JOIN TF_YMPN_PIPE_MF_RECORD_W e on d.ID = e.REPAIR_USER_ID " +
            "LEFT JOIN TF_YMPN_PIPE_MF_CONTENT_W f on e.ID = f.MF_ID " +
            "${ew.customSqlSegment} " +
            "GROUP BY a.id,a.ORDER_CODE,a.TYPE_ID,a.DISPATCHING_ID,a.NOTES,d.PHONE,d.RESP_USER,d.CREATE_TIME,e.REGION_NAME," +
            "e.CREATE_TIME,e.FACILITIES_TYPE, e.FACILITIES_CALIBER,e.CODE,e.VALVE_LOCATION " +
            "ORDER by a.id desc ")
    Page<MaintenanceMaintainVo> getMaintenanceMaintain(Page<MaintenanceTypeStatisticsVo> page, @Param(Constants.WRAPPER) QueryWrapper<Repairorder> queryWrapper);
    /**
     * 判断当前维修类型是否允许删除
     *
     * @return
     */
    @Select("select  count(*) " +
            "from TF_YWPN_REPAIRTYPE_D p LEFT JOIN TF_YWPN_REPAIRGOODS_W t1 on p.ID = t1.MATERIAL_TYPE " +
            "LEFT JOIN TF_YWPN_REPAIRORDER_W t2 on p.ID = t2.TYPE_ID " +
            "LEFT JOIN TF_YWPN_REPAIRPLANDETAIL_W t3 on p.ID = t3.TYPE_ID " +
            "${ew.customSqlSegment} ")
    Integer isDelete(@Param(Constants.WRAPPER) QueryWrapper<Repairorder> queryWrapper);

    @Select("SELECT id " +
            "FROM " +
            "tf_smpn_user_x")
    List<Long> getUserIdList();

    @Select("SELECT t1.id userId,t1.REAL_NAME userName,t2.id deptId,T2.name deptName " +
            "FROM TF_SMPN_USER_X t1 LEFT JOIN TF_SMPN_DEPARTMENT_X t2  " +
            "ON T1.DEPARTMENT_ID = t2.id " +
            "WHERE t1.id is not null ")
    List<PlanDisplayStatisticalVo> getUserNameAndDeptName();

    @Select("SELECT t.userId,t.planTotal,t.planOverNum,(t.planTotal-t.planOverNum) as planUndoneNum,decode(t.planTotal,0,0,round( (t.planOverNum)/t.planTotal*100,2)) planOverRate " +
            "FROM " +
            "(SELECT t2.RESP_USER userId, " +
            "count(t1.id) planTotal, " +
            "nvl(sum(CASE t1.PLAN_STATE " +
            "      WHEN '4' THEN 1 " +
            "      END ) ,0) AS planOverNum " +
            "FROM " +
            "  (SELECT ID,PLAN_STATE,PLAN_BEGINDATE,PLAN_ENDDATE FROM TF_YWPN_PLAN_W)t1 RIGHT JOIN (SELECT PLAN_ID,RESP_USER " +
            "FROM TF_YWPN_PLANCHILD_W)t2  " +
            "ON t2.PLAN_ID = t1.ID  " +
            "${ew.customSqlSegment} " +
            "GROUP BY t2.RESP_USER) t")
    List<PlanDisplayStatisticalVo> getPlanStatistical(@Param(Constants.WRAPPER) QueryWrapper<Plan> queryWrapper);

    @Select("SELECT CREATE_USER userId,count(id) buildNum " +
            "FROM TF_YWPN_BUILD_W " +
            "${ew.customSqlSegment} " +
            "GROUP BY CREATE_USER")
    List<PlanDisplayStatisticalVo> getPlanBuildStatistical(@Param(Constants.WRAPPER) QueryWrapper<Plan> queryWrapper);

    @Select("SELECT CREATE_USER userId,count(id) troubleNum " +
            "FROM TF_YWPN_TROUBLE_W " +
            "${ew.customSqlSegment} " +
            "GROUP BY CREATE_USER")
    List<PlanDisplayStatisticalVo> getPlanTroubleStatistical(@Param(Constants.WRAPPER) QueryWrapper<Plan> queryWrapper);

    @Select("SELECT t.userId,t.repairTotal,t.repairOverNum,(t.repairTotal-t.repairOverNum) as repairUndoneNum ," +
            "decode(t.repairTotal,0,0,round( (t.repairOverNum)/t.repairTotal*100,2)) repairOverRate " +
            "FROM " +
            "(SELECT t2.RESP_USER userId,count(t1.id) repairTotal, " +
            "nvl(sum(CASE t1.STATE " +
            "      WHEN '4' THEN 1 " +
            "      END ) ,0) AS repairOverNum " +
            "FROM TF_YWPN_REPAIRORDER_W t1 LEFT JOIN TF_YWPN_REPAIRUSER_W t2 " +
            "ON t1.ID = t2.REPAIR_ID " +
            "${ew.customSqlSegment} " +
            "GROUP BY t2.RESP_USER) t")
    List<PlanDisplayStatisticalVo> getMaintainStatistical(@Param(Constants.WRAPPER) QueryWrapper<Plan> queryWrapper);


    @Select("SELECT " +
            "P .userId,COUNT (P . ID) orderOverNum " +
            "FROM " +
            "( " +
            "SELECT s.id,s.lcjd,s.userId " +
            "FROM " +
            "( " +
            "SELECT " +
            "djry userId, " +
            "lcjd, " +
            "ID " +
            "FROM " +
            "TF_YWPN_GDINFO_W " +
            "UNION " +
            "SELECT " +
            "slry userId, " +
            "lcjd, " +
            "ID " +
            "FROM " +
            "TF_YWPN_GDINFO_W " +
            "UNION " +
            "SELECT " +
            "RZSLRY userId, " +
            "lcjd, " +
            "ID " +
            "FROM " +
            "TF_YWPN_GDINFO_W " +
            "UNION ALL " +
            "SELECT " +
            "SHSLRY userId, " +
            "lcjd, " +
            "ID " +
            "FROM " +
            "TF_YWPN_GDINFO_W " +
            "UNION " +
            "SELECT " +
            "ZJCZRY userId, " +
            "lcjd, " +
            "ID " +
            "FROM " +
            "TF_YWPN_GDINFO_W " +
            "UNION " +
            "SELECT " +
            "t2.PERSON_ID userId, " +
            "t1.lcjd, " +
            "t1. ID " +
            "FROM " +
            "( " +
            "SELECT " +
            "ID, " +
            "lcjd, " +
            "gdbh " +
            "FROM " +
            "TF_YWPN_GDINFO_W " +
            ") t1 " +
            "LEFT JOIN TF_YWPN_RESULTDISP_W t2 ON t1.gdbh = t2.gdbh " +
            "UNION " +
            "SELECT " +
            "t3.AUDIT_USER userId, " +
            "t1.lcjd, " +
            "t1. ID " +
            "FROM " +
            "( " +
            "SELECT " +
            "ID, " +
            "lcjd " +
            "FROM " +
            "TF_YWPN_GDINFO_W " +
            ") t1 " +
            "LEFT JOIN TF_YMPN_AUDIT_W t3 ON t1. ID = t3.AUDIT_PLAN_ID " +
            "WHERE " +
            "t3.AUDIT_TYPE = '12' " +
            ") s " +
            "RIGHT JOIN ( " +
            "SELECT " +
            "ID " +
            "FROM " +
            "TF_YWPN_GDINFO_W " +
            "WHERE " +
            "DJSJ >= #{startTime} " +
            "AND DJSJ <= #{endTime}  " +
            ") n ON s.ID = n.ID " +
            ") P " +
            "${ew.customSqlSegment} " +
            "GROUP BY " +
            "p.userId")
    List<PlanDisplayStatisticalVo> getOrderStatistical(@Param("startTime") Date startTime,@Param("endTime") Date endTime,@Param(Constants.WRAPPER) QueryWrapper<Plan> queryWrapper);
    @Select("SELECT ID " +
            "FROM TF_YWPN_ZLRZ_W " +
            "${ew.customSqlSegment} ")
    List<Long> getQualityIdTimeScopeList(@Param(Constants.WRAPPER) QueryWrapper<Plan> queryWrapper);
    @Select("SELECT m.userId,m.qualityZlrzOverNum qualityOverNum " +
            "FROM  " +
            "(SELECT count(p.id) qualityZlrzOverNum,p.userId  " +
            "FROM  " +
            "(SELECT s.id,s.CERTIFICATION_STATUS,s.userId  " +
            "FROM  " +
            "(SELECT id,CERTIFICATION_STATUS,DISPATCH_PERSON_ID userId  " +
            "FROM TF_YWPN_ZLRZ_W  " +
            "UNION  " +
            "SELECT id,CERTIFICATION_STATUS,CERTIFICATION_PERSON_ID userId  " +
            "FROM TF_YWPN_ZLRZ_W  " +
            "UNION  " +
            "SELECT id,CERTIFICATION_STATUS,ISSUE_PERSON_ID userId  " +
            "FROM TF_YWPN_ZLRZ_W  " +
            "UNION  " +
            "SELECT id,CERTIFICATION_STATUS,RECTIFY_PERSON_ID userId  " +
            "FROM TF_YWPN_ZLRZ_W) s  LEFT JOIN " +
            "(SELECT id " +
            "FROM TF_YWPN_ZLRZ_W " +
            "WHERE " +
            "DISPATCH_TIME >= #{startTime} " +
            "AND DISPATCH_TIME <= #{endTime} " +
            ") x on s.id = x.id )p " +
            "${ew.customSqlSegment} " +
            "GROUP BY p.userId) m  " +
            "WHERE  " +
            "m.userId is not null ")
    List<PlanDisplayStatisticalVo> getQualityOverZlrz(@Param("startTime") Date startTime,@Param("endTime") Date endTime,@Param(Constants.WRAPPER) QueryWrapper<Plan> queryWrapper);

    @Select("SELECT m.userId,m.qualityZlzgOverNum qualityOverNum  " +
            "FROM   " +
            "(SELECT count(p.id) qualityZlzgOverNum,p.userId   " +
            "FROM   " +
            "(SELECT s.id,s.userId,s.VALIDATE_STATUS " +
            "FROM " +
            "(SELECT t2.id,t1.AUDIT_PERSON_ID userId,t1.VALIDATE_STATUS   " +
            "FROM TF_YWPN_ZLZG_W t1 LEFT JOIN TF_YWPN_ZLRZ_W t2   " +
            "ON t1.CERTIFICATION_NUMBER = t2.CERTIFICATION_NUMBER   " +
            "UNION   " +
            "SELECT t2.id,t1.VERIFY_PERSON_ID userId,t1.VALIDATE_STATUS   " +
            "FROM TF_YWPN_ZLZG_W t1 LEFT JOIN TF_YWPN_ZLRZ_W t2   " +
            "ON t1.CERTIFICATION_NUMBER = t2.CERTIFICATION_NUMBER   " +
            "UNION   " +
            "SELECT t2.id,t1.VALIDATE_PERSON_ID userId,t1.VALIDATE_STATUS   " +
            "FROM TF_YWPN_ZLZG_W t1 LEFT JOIN TF_YWPN_ZLRZ_W t2   " +
            "ON t1.CERTIFICATION_NUMBER = t2.CERTIFICATION_NUMBER   " +
            ") s LEFT JOIN  " +
            "(SELECT t2.id  " +
            "FROM TF_YWPN_ZLZG_W t1 LEFT JOIN TF_YWPN_ZLRZ_W t2   " +
            "ON t1.CERTIFICATION_NUMBER = t2.CERTIFICATION_NUMBER    " +
            "WHERE " +
            "t2.DISPATCH_TIME >= #{startTime} " +
            "AND t2.DISPATCH_TIME <= #{endTime} " +
            " )x ON s.id = x.id )p " +
            "${ew.customSqlSegment}  " +
            "GROUP BY    " +
            "p.userId)m   " +
            "WHERE    " +
            "m.userId is not null ")
    List<PlanDisplayStatisticalVo> getQualityOverZlzg(@Param("startTime") Date startTime,@Param("endTime") Date endTime,@Param(Constants.WRAPPER) QueryWrapper<Plan> queryWrapper);

    @Select("SELECT t1.RESP_USER userId,count(1) planNum " +
            "FROM TF_YWPN_PLANCHILD_W t1 LEFT JOIN  TF_YWPN_PLAN_W t2  " +
            "on T1.PLAN_ID = T2.id " +
            "where  " +
            " t2.PLAN_BEGINDATE BETWEEN  #{startTime} and #{endTime} " +
            "and t2.PLAN_ENDDATE BETWEEN  #{startTime} and #{endTime} " +
            "GROUP BY T1.RESP_USER")
    List<PlanUserNumStatisticalVo> getUserPlanNumMap(@Param("startTime") Date startTime,@Param("endTime") Date endTime);
}
