/*
 * 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.plan.vo.PlanUserNumStatisticalVo;
import com.tofly.pojo.statement.vo.MaintenanceLogLedgerVo;
import com.tofly.pojo.statement.vo.MaintenanceMaintainVo;
import com.tofly.pojo.statement.vo.MaintenanceTypeStatisticsVo;
import com.tofly.pojo.maintain.Repairorder;
import com.tofly.pojo.maintain.Repairtype;
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
 */
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, " +
            "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, " +
            "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  " +
            "(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_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} ")
    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.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);
}
