package com.sw.mes.issue.dao;


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

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.issue.entity.YbIssue;

/**
* @Author
* @Description：
* @Date:
* @Modified By:
*/
public interface YbIssueDao extends BaseDao<YbIssue, Long> {

    YbIssue findByTrayNo(String trayNo);

    @Query(value = "SELECT\n" +
            "yi.* \n" +
            "FROM\n" +
            "\tyb_issue yi\n" +
            "\tLEFT JOIN yb_issue_detial yid ON yi.id = yid.issue_id\n" +
            "WHERE 1=1 \n" +
            "\tand (yi.tray_no = ?1 or ?1 is null)\n" +
            "\tand (yi.`status` = ?2 or ?2 is null)\n" +
            "\tand (yi.`time` >= ?3 or ?3 is null)\n" +
            "\tand (yi.`time` <= ?4 or ?4 is null)\n" +
            "\tand (yid.crystal_no = ?5 or ?5 is null)",
            countQuery = "SELECT\n" +
                    "count(1)" +
                    "FROM\n" +
                    "\tyb_issue yi\n" +
                    "\tLEFT JOIN yb_issue_detial yid ON yi.id = yid.issue_id\n" +
                    "WHERE 1=1 \n" +
                    "\tand (yi.tray_no = ?1 or ?1 is null)\n" +
                    "\tand (yi.`status` = ?2 or ?2 is null)\n" +
                    "\tand (yi.`time` >= ?3 or ?3 is null)\n" +
                    "\tand (yi.`time` <= ?4 or ?4 is null)\n" +
                    "\tand (yid.crystal_no = ?5 or ?5 is null)",nativeQuery = true)
    Page<Map<String, Object>> getPageList(String trayNo, Integer status, String startTime, String endTime, String crystalNo, Pageable pageable);


    @Query(value = "select \n" +
            "yid.section_no as code,\n" +
            "yid.product_level as productLevel\n" +
            "from plan_processing_order ppo  \n" +
            "LEFT JOIN produce_wip_storage pws on ppo.wip_storage_id=pws.id\n" +
            "LEFT JOIN yb_inspect_data yid on ppo.code=yid.section_no\n" +
            "where  \n" +
            "ppo.code=?1  \n" +
            "and \n" +
            "pws.process_name='圆棒检验'   \n" +
            "and pws.status=1  \n" +
            "and ppo.`status`=1 \n" +
            "and yid.is_cut_back=0\n" +
            "and yid.product_level is not null\n" +
            "ORDER BY ppo.create_time DESC \n" +
            "limit 1", nativeQuery = true)
    Map<String, Object> findByCode(String crystalNo);

    @Query(value = "SELECT yid.section_no as sectionNo, yid.product_level as productLevel \n" +
            "FROM plan_processing_order ppo \n" +
            "LEFT JOIN produce_wip_storage pws ON ppo.wip_storage_id = pws.id \n" +
            "LEFT JOIN yb_inspect_data yid ON ppo.code = yid.section_no \n" +
            "WHERE ppo.code IN(?1) \n" +
            "AND pws.process_name='圆棒检验' \n" +
            "AND pws.status=1 \n" +
            "AND ppo.`status`=1 \n" +
            "AND yid.is_cut_back=0 \n" +
            "AND yid.product_level IS NOT NULL \n" +
            "ORDER BY ppo.create_time DESC \n" +
            "LIMIT 1", nativeQuery = true)
    List<Map<String, Object>> findByCodes(List<String> sectionNos);

    @Modifying
    @Query(value = "update yb_issue set status=?1 where id= ?2", nativeQuery = true)
    void updateIssueStatus(Integer status, Long id);

    @Query(value = "select \n" +
            "yi.tray_no,yi.time,yi.status,yid.crystal_no,\n" +
            " yida.product_type_second,yida.dopant,yida.conductive_type,\n" +
            " yida.product_level,yida.diameter_min,yida.diameter_max,\n" +
            " yida.order_no,yida.material_code,yida.theory_weight,yida.actual_length\n" +
            "from yb_issue yi  \n" +
            "LEFT JOIN yb_issue_detial yid on yi.id=yid.issue_id\n" +
            "LEFT JOIN yb_inspect_data yida on yid.crystal_no=yida.section_no\n" +
            "where  \n" +
            " yi.`time` >= ?2 \n" +
            "and  yi.`time` <= ?3 \n"
            + "AND  IF(?1 <>'' AND ?1 IS NOT NULL,yi.`tray_no`=?1, 1=1 ) \n"
            , nativeQuery = true)
    List<Map<String, Object>> queryListByParam(String trayNo, String startTime, String endTime);
}