package com.rongwei.sfcommon.sys.dao;

import com.rongwe.scentity.domian.AccidentDo;
import com.rongwe.scentity.domian.AspAccidentReport;
import com.rongwe.scentity.domian.CheckTemplateDo;
import com.rongwe.scentity.vo.AccidentVo;
import com.rongwei.rwcommon.base.BaseDao;
import com.rongwei.rwcommon.base.R;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 事故表
 *
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2020-07-17 09:33:23
 */
public interface AccidentDao extends BaseDao<AccidentDo> {
    @Select("SELECT MAX(aa.ACCIDENTCODE) FROM asp_accident aa")
    String getCodeMAx();

    @Select("select sfi.FULLPATH from sys_file_item sfi WHERE  sfi.DELETED='0' AND sfi.ID=#{id}")
    AccidentVo getFilePathById(String id);

    @Select("   select\n" +
            "        sfi.FULLPATH\n" +
            "    from\n" +
            "        sys_file_item sfi LEFT JOIN\n" +
            "\t\t\t\tsys_file_folder sff ON sff.ID=sfi.FILEFOLDERID\n" +
            "\t\t\t\tWHERE sfi.DELETED='0'  AND  sff.FOLDERCODE='accident_word' ORDER BY sfi.MODIFYDATE DESC")
    List<AccidentVo> getFileList();

    @Select("select \n" +
            "(select COUNT(1) from asp_hidden_danger_track where DELETED='0' and YEAR(CREATEDATE)=YEAR(NOW()) and (WARNSTATUS != '过期' or WARNSTATUS is NULL))/(select COUNT(1) from asp_hidden_danger_track where DELETED='0' and YEAR(CREATEDATE)=YEAR(NOW())) AS 'RERATE',\n" +
            "(select COUNT(1) from asp_hidden_danger_track where DELETED='0' and YEAR(CREATEDATE)=YEAR(NOW()) AND HIDDENDANGERTYPE='20') AS 'ZDCOUNT'")
    Map<String, Object> getDataXTaskQuato();

    @Select("select (select COUNT(DISTINCT EMPID) from training_emp_exam a left join training_plan b on a.PLANID=b.ID " +
            "where a.EXAMSTATUS = '考试通过' and a.DELETED='0' and b.DELETED='0'  and YEAR(b.TRAININGSTARTTIME)=YEAR(NOW()))/(select COUNT(DISTINCT EMPID) " +
            "from training_emp_exam a left join training_plan b on a.PLANID=b.ID where a.DELETED='0' and b.DELETED='0' and YEAR(b.TRAININGSTARTTIME)=YEAR(NOW())) AS 'TRAINRATE'")
    Map<String, BigDecimal> getDataXtrainRate();

    @Select("select PLANNEDENDTIME from asp_contingency where STATE='1' and DELETED='0' and PARTICULARYEAR = YEAR(NOW())")
    List<Map<String, Object>> getDataXEmergency();

    @Select("<script>select COUNT(1) AS 'ACCIDENTCOUNT' from asp_accident " +
            "where DELETED='0' AND DATE_FORMAT(CREATEDATE,'%Y-%m') = DATE_FORMAT(NOW(),'%Y-%m') " +
            "<if test='accidenttype!=null'>" +
            "and ACCIDENTTYPE in (${accidenttype})" +
            "</if>" +
            "</script>")
    Map<String, Long> getDataXKjlAccident(@Param("accidenttype") String accidenttype);

    @Select("select (select COUNT(*) AS HASCHECKNUM from asp_point_check where DELETED='0' and CHECKUSERID is not null and YEAR(CREATEDATE) = YEAR(NOW())) / (select COUNT(*) AS TOTALCHECKNUM from asp_point_check where DELETED='0' and YEAR(CREATEDATE) = YEAR(NOW())) AS 'pointCheckRate'")
    Map<String, Double> getDataXPointCheck();

    @Select("SELECT * FROM sys_organization where DELETED = 0 AND PID = '-1'")
    List<Map<String, String>> selectFactory();

    //    @Select({"<script>" +
//            "SELECT\n" +
//            "\tso.ID AS 'departid',\n" +
//            "\tso.FULLNAME AS 'departname',\n" +
//            "\tsum(IFNULL( suo.totalPeople, 0 )) AS 'departpeople',\n" +
//            "\tsum(IFNULL( b.count, 0 )) AS 'count',\n" +
//            "\tIFNULL(round( sum( IFNULL( b.count, 0 ))/ sum( IFNULL( suo.totalPeople, 0 ))* 100, 2 ),0.00) AS 'value'\n" +
//            "FROM\n" +
//            "\tsys_organization so\n" +
//            "\tLEFT JOIN sys_organization so2 ON ( ( so2.FULLPID LIKE CONCAT( '%', so.ID, '%' )) OR so2.ID = so.ID ) \n" +
//            "\tAND so2.DELETED = 0\n" +
//            "\tLEFT JOIN (\n" +
//            "\tSELECT\n" +
//            "\t\taa.ACCIDENTDEPT,\n" +
//            "\t\taa.ACCIDENTDEPTNAME,\n" +
//            "\t\tcount( 1 ) AS 'count' \n" +
//            "\tFROM\n" +
//            "\t\tasp_accident aa \n" +
//            "\tWHERE\n" +
//            "\t\taa.DELETED = 0 \n" +
//            "\t\tAND aa.DECLAREJOBINJURED = '1' \n" +
//            "\t\t<if test = 'month != -1'>AND MONTH ( aa.HAPPENDATE ) = #{month}</if> \n" +
//            "\t\tAND YEAR ( aa.HAPPENDATE ) = #{year} \n" +
//            "\t\t<if test = 'tenantId != null and tenantId != \"\"'>AND aa.TENANTID = #{tenantId}</if>\n" +
//            "\tGROUP BY\n" +
//            "\t\taa.ACCIDENTDEPT \n" +
//            "\t) b ON b.ACCIDENTDEPT = so2.ID \n" +
//            "\tAND so.DELETED = 0\n" +
//            "\tLEFT JOIN ( SELECT ORGID, count( 1 ) AS totalPeople FROM sys_user_org WHERE DELETED = 0 GROUP BY ORGID ) suo ON so2.ID = suo.ORGID \n" +
//            "WHERE\n" +
//            "\tso.ORGTYPE = 'chejian' \n" +
//            "\t<if test = 'tenantId != null and tenantId != \"\"'>AND so.FULLPID like CONCAT('%',#{tenantId},'%')</if>\n" +
//            "GROUP BY\n" +
//            "\tso.ID"+
//            "</script>"})
    List<AspAccidentReport> selectReport(int year, int month, String tenantId);

    @Select({"<script>" +
            "SELECT\n" +
            "\t* \n" +
            "FROM\n" +
            "\tasp_accident_report \n" +
            "WHERE\n" +
            "\tDELETED = 0 \n" +
            "\tAND `YEAR` = #{year}" +
            "\t<if test = 'tenantId != null and tenantId != \"\"'>and TENANTID = #{tenantId}</if>" +
            "</script>"})
    List<AspAccidentReport> selectReportByYear(Integer year, String tenantId);

    @Select({"<script>" +
            "SELECT\n" +
            "\tso.ID AS 'departId',\n" +
            "\tso.FULLNAME AS 'departName',\n" +
            "\tCAST(ifnull(sum(suo.totalPeople),0) AS UNSIGNED) AS 'totalPeople'\n" +
            "FROM\n" +
            "\tsys_organization so\n" +
            "\tLEFT JOIN sys_organization so2 ON ( ( so2.FULLPID LIKE CONCAT( '%', so.ID, '%' )) OR so2.ID = so.ID ) \n" +
            "\tAND so2.DELETED = 0\n" +
            "\tLEFT JOIN ( SELECT ORGID, count( 1 ) AS totalPeople FROM sys_user_org WHERE DELETED = 0 GROUP BY ORGID ) suo ON so2.ID = suo.ORGID \n" +
            "WHERE\n" +
            "\tso.ORGTYPE = 'chejian' \n" +
            "\t<if test = 'tenantId != null and tenantId != \"\"'>AND so.FULLPID like CONCAT('%',#{tenantId},'%')</if>\n" +
            "GROUP BY\n" +
            "\tso.ID" +
            "</script>"})
    List<Map<String, Object>> getOrgMap(@Param("tenantId") String tenantId);

}
