package com.huimv.admin.repo.secondary;

import com.huimv.admin.domain.secondary.MachineControlEntity;
import com.huimv.admin.vo.MachineControlVO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

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

/**
 * @Author yinhao
 * @Date 2021/4/1 18:12
 * @Description 料控管理Dao
 */
public interface MachineControlRepository extends JpaRepository<MachineControlEntity, Integer>, JpaSpecificationExecutor<MachineControlEntity> {

//    MachineControlEntity findByJqid(String jqId);

    @Query(nativeQuery = true,value = "SELECT\n" +
            "    lk.id,\n" +
            "    zs.id zsId,\n" +
            "    ZSMC,\n" +
            "    lk.jqid,\n" +
            "    no1_dlstart_time1 no1DlstartTime1,\n" +
            "    no1_dlstart_time2 no1DlstartTime2,\n" +
            "    no1_dlstart_time3 no1DlstartTime3,\n" +
            "    no1_dlstart_time4 no1DlstartTime4,\n" +
            "    no2_dlstart_time1 no2DlstartTime1,\n" +
            "    no2_dlstart_time2 no2DlstartTime2,\n" +
            "    no2_dlstart_time3 no2DlstartTime3,\n" +
            "    no2_dlstart_time4 no2DlstartTime4,\n" +
            "    no3_dlstart_time1 no3DlstartTime1,\n" +
            "    no3_dlstart_time2 no3DlstartTime2,\n" +
            "    no3_dlstart_time3 no3DlstartTime3,\n" +
            "    no3_dlstart_time4 no3DlstartTime4,\n" +
            "    no4_dlstart_time1 no4DlstartTime1,\n" +
            "    no4_dlstart_time2 no4DlstartTime2,\n" +
            "    no4_dlstart_time3 no4DlstartTime3,\n" +
            "    no4_dlstart_time4 no4DlstartTime4\n" +
            "FROM\n" +
            "  da_zsda zs\n" +
            "  JOIN da_jqda jq\n" +
            "    ON zs.`ID` = jq.`ZSID`\n" +
            "  JOIN da_lkpz lk\n" +
            "    ON jq.`JQID` = lk.`jqid`\n" +
            "WHERE zs.MCID = 39 and zs.ZFBZ = 0 and jq.ZFBZ = 0 and ZSMC LIKE CONCAT('%',?3,'%') LIMIT ?1,?2")
    List<Map> getMachineControlInfo(Integer start, Integer pageSize, String keyword);

//    @Query(nativeQuery = true,value = "SELECT zs.`ZSMC`,lk.* FROM da_zsda  zs JOIN da_jqda  jq ON zs.`ID`=jq.`ZSID` JOIN da_lkpz lk ON jq.`JQID`=lk.`jqid` where zs.ZSMC like %?3% limit ?1,?2")
//    List<Map> findMachineControlInfo(Integer start,Integer pageSize,String keyword);

    @Query(nativeQuery = true,value = "SELECT count(*) FROM da_zsda  zs JOIN da_jqda  jq ON zs.`ID`=jq.`ZSID` and zs.MCID = 39 and zs.ZFBZ = 0 and jq.ZFBZ = 0 JOIN da_lkpz lk ON jq.`JQID`=lk.`jqid` where zs.ZSMC like %?1%")
    Long countTotalSize(String keyword);

    /**
     * 新增时的猪舍名称下拉框
     * @return
     */
    @Query(nativeQuery = true,value = "SELECT zs.ZSMC,zs.id zsId,jq.JQID jqid  FROM da_zsda zs JOIN da_jqda jq ON zs.ID = jq.ZSID WHERE zs.MCID = 39 and zs.ZFBZ = 0 and jq.ZFBZ = 0 and jq.JQID NOT IN (SELECT jqid FROM da_lkpz lk)")
    List<Map<String,Object>> selectZsList();
}
