/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.dm.printingRequest.repository;

import me.zhengjie.modules.dm.printingRequest.domain.DmPrintingRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @author xwx
* @date 2021-03-15
**/
public interface DmPrintingRequestRepository extends JpaRepository<DmPrintingRequest, Long>, JpaSpecificationExecutor<DmPrintingRequest> {

    /**
     * 查询最大的code
     * @return
     */
    @Query(value = "select max(task_code) from dm_printing_request", nativeQuery = true)
    String getMaxCode();

    @Modifying
    @Query(value = "update dm_printing_request set task_id =:#{#resources.taskId} ,proc_inst_id =:#{#resources.procInstId} where id =:#{#resources.id} ",nativeQuery = true )
    void updatePringTask(@Param(value = "resources") DmPrintingRequest resources);

    @Query(value = "select GROUP_CONCAT(DISTINCT(approval_user)) as userIds from dm_printing_approval where is_new = '1' and request_id = ?1 ",nativeQuery = true )
    Map<String,Object> selectPrintingApproveUserNames(Long id);

    @Modifying
    @Query(value = "update dm_printing_request set status = '00' ,approval_user = null,approval_remark=null where id = ?1 ",nativeQuery = true )
    void reset(Long id);

    @Query(value = "select status from dm_printing_request where id = ?1",nativeQuery = true)
    String findSubmitBeforeStatus(Long id);

    @Query(value = "SELECT COUNT(1) FROM dm_printing_request WHERE printer_code = ?1",nativeQuery = true)
    Integer selecIsNotDevice(Long floorId);

    @Query(value = "SELECT name FROM dm_device WHERE id in (?1)",nativeQuery = true)
    List selectDeviceName(Long[] floorId);

    @Query(value = "SELECT r.id,r.task_name AS taskName,u.dept_id AS deptId,d.`name` AS `deptName`,u.`name` AS `userName`,r.create_time AS createTime," +
            "r.remark,r.file_page_num AS filePageNum,de.`name` AS `deviceName`,ls.suffix,real_name AS realName,a.approval_remark AS approvalRemark " +
            "FROM dm_printing_request r LEFT JOIN dm_printing_queue q ON r.id = q.request_id " +
            "LEFT JOIN dm_user u ON r.request_user = u.id LEFT JOIN sys_dept d ON u.dept_id = d.dept_id " +
            "LEFT JOIN dm_device de ON r.printer_code = de.id LEFT JOIN tool_local_storage ls ON r.fileid = ls.storage_id " +
            "LEFT JOIN dm_printing_approval a ON r.id = a.request_id WHERE request_user = ?3 AND if(?2 is not null and ?2 !='',r.`status` = ?2,1=1) AND if(?1 is not null and ?1 !='',q.`status` = ?1,1=1) GROUP BY r.create_time DESC "
            ,countQuery = "select count(*) from (SELECT r.id,r.task_name AS taskName,u.dept_id AS deptId,d.`name` AS `deptName`,u.`name` AS `userName`,r.create_time AS createTime," +
            "r.remark,r.file_page_num AS filePageNum,de.`name` AS `deviceName`,ls.suffix,real_name AS realName,a.approval_remark AS approvalRemark " +
            "FROM dm_printing_request r LEFT JOIN dm_printing_queue q ON r.id = q.request_id " +
            "LEFT JOIN dm_user u ON r.request_user = u.id LEFT JOIN sys_dept d ON u.dept_id = d.dept_id " +
            "LEFT JOIN dm_device de ON r.printer_code = de.id LEFT JOIN tool_local_storage ls ON r.fileid = ls.storage_id " +
            "LEFT JOIN dm_printing_approval a ON r.id = a.request_id " +
            "WHERE request_user = ?3 AND if(?2 is not null and ?2 !='',r.`status` = ?2,1=1) AND if(?1 is not null and ?1 !='',q.`status` = ?1,1=1)) a",nativeQuery = true)
    Page<Map<String,Object>> findByRequestUser(String status, String printingRequest, String requestUser, Pageable pageable);

    @Query(value = "select pr.request_user as requestUser,u.dept_id as deptId,ls.real_name realName,pr.file_page_num as filePageNum,if(pr.`status`=00,'待审核','待审核') as printStatus from dm_printing_request as pr left join dm_user as u on pr.request_user = u.id left join tool_local_storage as ls on pr.fileid = ls.storage_id where pr.printer_code = :deviceId and pr.`status` = '00'",nativeQuery = true )
    List<Map<String,Object>> requestListByDeviceId(@Param("deviceId")Long deviceId);

    @Query(value = "SELECT count(1) FROM dm_printing_request where status = '00' ",nativeQuery = true)
    Long findRequestCount();

    @Query(value = "SELECT d.* FROM dm_printer_file d  left join  dm_printing_request r  on d.printing_id = r.id where r.status = '00'",nativeQuery = true)
    List<Map<String,Object>> findRequestInfo();
}
