package com.rongwei.bscommon.sys.dao;

import com.rongwei.bsentity.request.IssuesReportRequest;
import com.rongwei.bsentity.vo.SrmActionPlanVo;
import com.rongwei.bsentity.domin.SrmIssuesListDo;
import com.rongwei.bsentity.vo.SrmFormCountVo;
import com.rongwei.bsentity.vo.SrmIssuesListVo;
import com.rongwei.rwcommon.base.BaseDao;
import com.rongwei.rwcommonentity.commonservers.domain.SysFileItemDo;
import dto.DeparterAndWorkShoperVo;
import dto.SysNoticeVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * PSL问题
 * @author Trj
 */
@Mapper
public interface SrmIssuesListDao extends BaseDao<SrmIssuesListDo> {

    // 获取 问题ID/任务SPA并签提交时间
    List<SrmIssuesListDo> getProblemByTaskRun();

    /**
     * 查询行动计划表最晚实际完成时间
     * @param issuesReportRequest
     * @return
     */
    List<SrmActionPlanVo> getMaxRealEdDate(@Param("issuesReportRequest") IssuesReportRequest issuesReportRequest);

    /**
     * 查询行动计划表最晚计划完成时间
     * @param issuesReportRequest
     * @return
     */
    List<SrmActionPlanVo> getMaxTargetCompletionDate(@Param("issuesReportRequest") IssuesReportRequest issuesReportRequest);

    /**
     * 表单总数
     * @param issuesReportRequest
     * @return
     */
    List<SrmFormCountVo> getFormCount(@Param("issuesReportRequest") IssuesReportRequest issuesReportRequest);

    List<SrmIssuesListDo> getListByMesId(@Param("mesIds")List<String> mesIds);

    void insertFile(@Param("model") SysFileItemDo sysFileItemDo);

    @Select("SELECT sna.*,GROUP_CONCAT(snau.RECIPIENTID) as 'recipientIds' FROM sys_notify_announce sna LEFT JOIN sys_notify_announce_user snau ON sna.ID = snau.ANNOUNCEID AND snau.DELETED = 0 where sna.DELETED = 0 AND sna.NOTIFYTITLE like CONCAT('生产问题--','%') AND sna.CREATEDATE >= DATE_FORMAT(NOW(),'%Y-%m-%d %H:00:00') AND sna.CREATEDATE <= NOW() GROUP BY sna.ID")
    List<SysNoticeVo> selectNoticeByProduct();

    void updateNoticeByProduct(@Param("idList") List<String> idList);

    /**
     * 查询部门长和车间主任
     * @param endOrgId
     * @return
     */
    @Select("select DISTINCT su.ID AS 'id',su.`NAME` AS 'name' from sys_user su LEFT JOIN sys_user_org suo ON suo.DELETED = 0 AND suo.USERID = su.ID LEFT JOIN sys_organization so ON so.DELETED = 0 AND so.ID = suo.ORGID LEFT JOIN sys_user_role sur ON sur.DELETED = 0 AND sur.USERID = su.ID LEFT JOIN sys_role sr ON sr.DELETED = 0 AND sr.ID = sur.ROLEID where su.DELETED = 0 AND (so.FULLPID LIKE CONCAT('%',#{endOrgId},'%') OR so.ID = #{endOrgId}) AND sr.ID IN ('b104293d47e34df8847d1634c29ab031','16963f62b94149d9ae16eabc9248999b') ")
    List<DeparterAndWorkShoperVo> selectDeparterAndWorkShoper(@Param("endOrgId") String endOrgId);

    @Select("SELECT\n" +
            "\ta.*,\n" +
            "\tb.TASKSTATUS,\n" +
            "\tb.CREATEDATE 'nodetime',\n" +
            "\tIFNULL( b.TASKNAME, \"问题登记人\" ) TASKNAME,\n" +
            "\tb.OPERATIONID \n" +
            "FROM\n" +
            "\tsrm_issues_list a\n" +
            "\tLEFT JOIN wf_run_task b ON a.PROCESSINSTID = b.PROCESSINSTID \n" +
            "\tAND b.TASKSTATUS IN ( '10', '20' ) \n" +
            "\tAND b.DELETED = '0' \n" +
            "WHERE\n" +
            "\ta.DELETED = '0' \n" +
            "\tAND IFNULL( a.PROCESSINSTSTATUS, '10' )= '10'\n")
    List<SrmIssuesListVo> selectTimeOutData();

}
