package com.crunii.micro.domain.repo.main;

import com.crunii.micro.domain.common.BaseRepo;
import com.crunii.micro.domain.entity.main.TsmWorker;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author 田平 create 2019-01-28 05:01:09
 */
@Repository
public interface TsmWorkerRepo extends BaseRepo<TsmWorker, Integer> {

    /**
     * 通过电话号码查询worker
     * @param mobile 电话号
     * @return worker
     */
    TsmWorker findByMobile(String mobile);

    /**
     * 通过电话号码查询worker（不查询状态为0）
     * @param mobile 电话号
     * @param stateValid 状态
     * @return worker
     */
    TsmWorker findByMobileAndState(String mobile, int stateValid);

    /**
     * 通过loginId查询worker（不查询状态为0）
     * @param loginId
     * @param state
     * @return
     */
    TsmWorker findByLoginIdAndState(Integer loginId,Integer state);

    /**
     * 根据手机号查询
     * @return
     */
    @Query(value ="select * from tsm_worker where mobile=?1 and login_id!=?2",nativeQuery = true)
    List<TsmWorker> findAllWorkerByMobile(String mobile,Integer loginId);


    /**
     * 查询状态为1的用户
     * @param state
     * @return
     */
    List<TsmWorker> findByState(int state);




    /**
     * 查询当前用户是否是评审员,以及是否签名
     * @param workerId
     * @return
     */
    @Query(value = "select b.reviewer_id,a.* from tsm_worker a inner join (select reviewer_id from mth_round_reviewer " +
            " where reviewer_id is not null group by reviewer_id) b on a.login_id=b.reviewer_id where a.login_id=?1 and signature_file_id is not null",nativeQuery = true)
    TsmWorker findByReviewerId(Integer workerId);

    /**
     * 根据姓名查询状态为1的用户
     * @param workerName 姓名
     * @param state
     * @return
     */
    List<TsmWorker> findByWorkerNameAndState(String workerName,int state);

    @Query(value = "select  * from tsm_worker where mobile=:request or login_code=:request",nativeQuery = true)
    List<TsmWorker> findByMobileOrCode(@Param("request") String request);

    @Query(value = "select  * from tsm_worker where mobile=:mobile and state !=0",nativeQuery = true)
    List<TsmWorker> findAllByMobile(@Param("mobile") String mobile);

    /**
     * 根据登录账号查询用户
     * @param loginCode 登录账户
     * @return List<TsmWorker>
     */
    List<TsmWorker> findByLoginCode(String loginCode);

    /**
     * 根据所属部门查询用户
     * @param department
     * @return
     */
    @Query(value = "select  * from tsm_worker where department=:department and state =1",nativeQuery = true)
    List<TsmWorker> findAllByDepartment(@Param("department") String department);

    /**
     * 根据code查询用户，排除自己
     * @param loginCode
     * @return
     */
    @Query(value = "select  * from tsm_worker where login_code=?1 and login_id !=?2",nativeQuery = true)
    List<TsmWorker> findByLoginCodeNotSelf(String loginCode,Integer loginId);


    @Query(value = "select distinct mobile from tsm_worker where mobile is not null ",nativeQuery = true)
    List<String> findAllMobile();

}
