package com.ankaibei.workFlow.webadmin.ankaibei.mapper;

import com.ankaibei.workFlow.webadmin.ankaibei.entity.ProjectInfoEntity;
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 javax.transaction.Transactional;
import java.util.List;
import java.util.Map;

/**
 * @author lwq
 * @version 1.0
 * @description:
 * @date 2022/9/14 11:13
 */
public interface ProjectInfoMapper extends JpaRepository<ProjectInfoEntity, Integer>, JpaSpecificationExecutor<ProjectInfoEntity> {

    @Query(value = "SELECT pro.project_id projectId,pro.project_name projectName,pro.project_stage projectStage,pro.project_status projectStatus,pro.project_principal projectPrincipal,pro.create_date createDate,pro.flow_task_status flowTaskStatus ,pro.process_instance_id processInstanceId, pro.process_definition_id processDefinitionId FROM project_info pro WHERE if( ?1 !='', pro.project_status=?1 , 1=1) and if( ?2 !='', pro.project_name like %?2% , 1=1) and pro.is_delete='1' order by pro.create_date desc",
            countQuery = "SELECT count(1) FROM project_info pro WHERE if( ?1 !='', pro.project_name like %?1% , 1=1) and pro.is_delete='1' ",
            nativeQuery = true)
    Page<Map<String, String>> findProjectInfo(String projectStatus, String projectName, Pageable page);

    @Transactional
    @Modifying
    @Query(value = "update project_info pro SET pro.project_stage = ?1,pro.project_status = ?2 WHERE pro.project_id = ?3", nativeQuery = true)
    void updateProject(String projectStage, String projectStatus, Integer projectId);

    @Transactional
    @Modifying
    @Query(value = "update project_info SET flow_task_status = ?1 WHERE project_id = ?2", nativeQuery = true)
    void updateProjectFlowStatus(String flowTaskStatus, Integer projectId);

    @Transactional
    @Modifying
    @Query(value = "update project_info SET is_delete='-1' WHERE project_id = ?1", nativeQuery = true)
    void deleteProject(Integer projectId);


    List<ProjectInfoEntity> findByProjectPrincipalEqualsAndIsDeleteEquals(String userId, String isDeleted);

    @Transactional
    @Query(value = "SELECT COUNT(*) AS 'num', DATE_FORMAT(create_date,'%Y-%m-%d') AS 'time'  FROM project_info WHERE DATE_FORMAT(create_date, '%Y-%m-%d') " +
            "IN (?1) and is_delete='1' and flow_task_status='3' GROUP BY DATE_FORMAT(create_date, '%Y-%m-%d') ORDER BY DATE_FORMAT(create_date, '%Y-%m-%d') DESC", nativeQuery = true)
    List<Map<String, Object>> findProjectNumber(List<String> twoDaysDayDes);

    @Transactional
    @Query(value = "SELECT DATE_FORMAT(create_date,'%Y-%m') AS 'time',project_principal projectPrincipal" +
            " FROM project_info WHERE create_date>= ?1 and  create_date<= ?2 and is_delete='1' and flow_task_status='3' "
            , nativeQuery = true)
    List<Map<String, Object>> getProjectNumberOfMonth(String startDate, String endDate);


    /**
     * 根据名称查询用户信息
     *
     * @param projectName
     */
    @Query(value = "SELECT pro.*  FROM project_info pro WHERE if( :projectName !='', pro.project_name like CONCAT('%',:projectName,'%') , 1=1) and is_delete='1'  order by pro.create_date desc",
            //countQuery = "SELECT count(1) FROM project_info pro WHERE if( ?1 !='', pro.project_name like %?1% , 1=1) ",
            nativeQuery = true)
    List<ProjectInfoEntity> findAllByProjectName(@Param("projectName") String projectName);

    /**
     * 根据编码查询项目信息
     *
     * @param projectCode
     */
    @Query(value = "SELECT *  FROM project_info  WHERE project_code=?1 and is_delete='1'",
            //countQuery = "SELECT count(1) FROM project_info pro WHERE if( ?1 !='', pro.project_name like %?1% , 1=1) ",
            nativeQuery = true)
    ProjectInfoEntity  findProjectInfoByCode(@Param("projectCode") String projectCode);
}
