/*
 * Author: Zev
 * 生成mapper
 */
package com.tofly.gpsboot.mapper.trouble;

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.statement.dto.StatementDto;
import com.tofly.pojo.statement.vo.TroubleRecordLedgerVo;
import com.tofly.pojo.trouble.Trouble;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 隐患上报信息表
 *
 * @author admin
 * @date Thu Mar 18 00:00:00 CST 2021
 */
@Mapper
public interface TroubleMapper extends BaseMapper<Trouble> {
    @Select("select p.PIPE_NAME pipeName, p.LOCATION location, p.ADDRESS address,p.TYPE_ID troubleTypeId,v.name troubleTypeName, m.id rangeId,m.name rangeName,p.NOTES note, p.CREATE_TIME troubleTime,p.ISBUILD isBuild,n.SGDW sgdw,n.GDFZR gdfzr, " +
            "n.PHONE phone,p.SUGGEST suggest,t.AUDIT_RESULT isSuggest,t.AUDIT_TIME eliminateTime,t.NOTES eliminateTroubleNotes,p.SUBMIT_USER submitUserid,p.SUBMIT_TIME submitTime , " +
            "( select REAL_NAME  " +
            "        from TF_SMPN_USER_X " +
            "        where id= p.SUBMIT_USER ) as submitUserName, " +
            "(select NAME " +
            "        from TF_SMPN_DEPARTMENT_X " +
            "        where id = " +
            "              (select DEPARTMENT_ID " +
            "               from TF_SMPN_USER_X " +
            "               where id = p.SUBMIT_USER )) as deptName   " +
            "from TF_YWPN_TROUBLE_W p LEFT JOIN ( SELECT * FROM TF_YWPN_TOUBLECLEAR_W WHERE CREATE_TIME IN ( SELECT MAX( CREATE_TIME ) AS CREATE_TIME FROM TF_YWPN_TOUBLECLEAR_W GROUP BY TOUBLE_ID ) ) t on p.ID = t.TOUBLE_ID " +
            "LEFT JOIN TF_YWPN_BUILD_W n on p.BUILD_ID = n.ID " +
            "LEFT JOIN TF_YWPN_TOUBLETYPE_D v on v.ID = p.TYPE_ID " +
            "LEFT JOIN TF_YWPN_TOUBLERANGE_D m on m.ID = p.TOUBLE_RANGE_ID " +
            "${ew.customSqlSegment} " +
            "order by troubleTime DESC")
    Page<TroubleRecordLedgerVo> searchWeeklyPage(Page<TroubleRecordLedgerVo> page, @Param(Constants.WRAPPER) QueryWrapper<StatementDto> queryWrapper);
}
