/*
*  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.ykt.repository;

import me.zhengjie.ykt.domain.DmPlateNumber;
import me.zhengjie.ykt.service.dto.DmPlateNumberDto;
import me.zhengjie.ykt.service.dto.DmPlateNumberQueryCriteria;
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 org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @website https://el-admin.vip
* @author zhirong
* @date 2021-11-10
**/
public interface DmPlateNumberRepository extends JpaRepository<DmPlateNumber, Long>, JpaSpecificationExecutor<DmPlateNumber> {

    /* 多表查询 */
//    @Modifying
    @Query(value = "SELECT dm_user.*,dm_card.*,dm_plate_number.* FROM dm_user,dm_card,dm_plate_number where dm_user.card_id \n" +
            "= dm_card.id and dm_card.id = dm_plate_number.id",nativeQuery = true)
//    @Modifying
//    @Query(value = "select u.*,c.*,n.* from dm_user u, dm_card c, dm_plate_number n where u.card_id=c.id and c.id = n.id",nativeQuery = true)
    Page<DmPlateNumber> getAllPlateNumber(Pageable pageable);

    /* 搜索 */
    @Modifying
    @Query(value = "SELECT `name`, user_type, card_number, contacts plate_number, start_time, end_time, effective_state, registration_status " +
            " FROM dm_user,dm_card,dm_plate_number  where dm_user.card_id = dm_card.id and dm_card.id = dm_plate_number.id " +
            " and plate_number LIKE CONCAT (:#{#resources.plateNumber},'%') and  card_number = :#{#resources.dmCard.cardNumber} and `name`= :#{#resources.dmCard.dmUser.name} and registration_status = :#{#resources.registrationStatus} " +
            " and effective_state = :#{#resources.effectiveState} and start_time = :#{#resources.startTime}",nativeQuery = true)
    List<DmPlateNumber> findAllBysousuo(@Param(value = "resources") DmPlateNumber resources);

    /* 根据一卡通卡号查询 */
    @Query(value = "select dm_user.`name`,user_type,contacts,dept_name,plate_number,start_time,end_time,effective_state, "+
            " registration_status,start_time from dm_user,dm_card,dm_plate_number,dm_dept "+
            " where dm_user.card_id = dm_card.id and dm_card.id = dm_plate_number.id and dm_dept.id = dm_user.dept_id "+
            " and dm_card.card_number = :card_number",nativeQuery = true)
    DmPlateNumber findDmPlateNumberByCardNumber(@Param("card_number") Long cardNumber);

    @Query(value = "SELECT a.`id`, a.`user_number`, `plate_number`, `start_time`, `end_time`, a.`card_number`, " +
            "`effective_state`, `registration_status`,a.`user_name`,a.`user_phone`, a.`create_by`, a.`update_by`, a.`create_time`, a.`enabled`, " +
            "a.`update_time`, a.`remark`,a.`vehicle_type`, null as `dmUser.id`, b.`name`, null as `dmUser.user_number`, " +
            "null as `dmUser.sex`, `contacts`, null as `dmUser.email`, null as `dmUser.dept_id`, " +
            "null as `dmUser.status`, null as `dmUser.usertype`, null as `dmUser.picture`, " +
            "null as `dmUser.remark`,a.`type`, null as `dmUser.is_approver`, null as `dmUser.create_by`," +
            " null as `dmUser.update_by`, null as `dmUser.create_time`, null as `dmUser.update_time`, " +
            "null as `dmUser.enabled`, null as `dmUser.company` FROM dm_plate_number a " +
            "LEFT JOIN dm_user b ON a.id = b.user_number WHERE vehicle_type = '00' ",nativeQuery = true)
    List<DmPlateNumber> yuePlateRepository();

    @Query(value = "SELECT a.`id`, a.`user_number`, `plate_number`, `start_time`, `end_time`, a.`card_number`, " +
            "`effective_state`, `registration_status`,a.`user_name`,a.`user_phone`, a.`create_by`, a.`update_by`, a.`create_time`, a.`enabled`, " +
            "a.`update_time`, a.`remark`,a.`type`,a.`vehicle_type`, null as `dmUser.id`, b.`name`, null as `dmUser.user_number`, " +
            "null as `dmUser.sex`, `contacts`, null as `dmUser.email`, null as `dmUser.dept_id`, " +
            "null as `dmUser.status`, null as `dmUser.usertype`, null as `dmUser.picture`, " +
            "null as `dmUser.remark`, null as `dmUser.is_approver`, null as `dmUser.create_by`," +
            " null as `dmUser.update_by`, null as `dmUser.create_time`, null as `dmUser.update_time`, " +
            "null as `dmUser.enabled`, null as `dmUser.company` FROM dm_plate_number a " +
            "LEFT JOIN dm_user b ON a.id = b.user_number WHERE vehicle_type = '02' ",nativeQuery = true)
    List<DmPlateNumber> mfPlateRepository();

    @Query(value = "SELECT * FROM dm_plate_number WHERE type = '01' AND card_number = :cardNumber " +
            "ORDER BY create_time ASC LIMIT 0,1 ",nativeQuery = true)
    DmPlateNumber takesLongestReplace(@Param("cardNumber") Long cardNumber);

    @Query(value = "SELECT COUNT(1) FROM dm_plate_number WHERE type = '01' AND card_number = :cardNumber ",nativeQuery = true)
    int countUserPlate(@Param("cardNumber") Long cardNumber);

    @Query(value = "SELECT * FROM dm_plate_number WHERE card_number = :cardNumber order by create_time desc",nativeQuery = true)
    List<DmPlateNumber> getDmPlateNumberByCard(@Param("cardNumber") String cardNumber);

    /**
     * 状态修改
     * @param enabled
     */
    @Transactional
    @Modifying
    @Query(value = " update dm_plate_number set enabled = ?1 WHERE type = '00' ",nativeQuery = true)
    void updateEnabled(String enabled);

    @Query(value = "select is_approve from dm_plate_number_approve",nativeQuery = true)
    String queryApprove();

    /**
     * 审批状态修改
     * @param isApprove
     */
    @Transactional
    @Modifying
    @Query(value = "update dm_plate_number_approve set is_approve = ?1",nativeQuery = true)
    void updateApprove(String isApprove);

    @Query(value = "select * from dm_plate_number where task_id = ?1",nativeQuery = true)
    DmPlateNumber findDmPlateNumberByTaskId(String taskId);


    @Query(value = "SELECT plate.*  FROM dm_plate_number plate ",nativeQuery = true)
    @Transactional
    List<DmPlateNumber> queryDmPlateNumberAll();

    @Query(value = "UPDATE  dm_plate_number SET card_number = ?1, user_name = ?2,user_phone = ?3  where id = ?4 ",nativeQuery = true)
    @Modifying
    @Transactional
    int updateAll(String userNumber, String name, String contacts,Long id);

    @Query(value = "select * from dm_plate_number where user_number = ?1",nativeQuery = true)
    DmPlateNumber findByUserId(String userId);
}