package com.sw.mes.yb.pad.dao;

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

import org.springframework.data.jpa.repository.Query;

import com.sw.mes.core.base.BaseDao;
import com.sw.mes.yb.pad.entity.ProductionPaintingLine;

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


    @Query(value = "SELECT * FROM `production_painting_line` WHERE crystal_no = ?1 ORDER BY id DESC LIMIT 1", nativeQuery = true)
    ProductionPaintingLine findByCrystalNo(String crystalNo);

    @Query(value = "SELECT * FROM `production_painting_line` WHERE vehicle_code = ?1", nativeQuery = true)
    List<ProductionPaintingLine> findByVehicleCodes(String vehicleCode);

    @Query(value = "select \n" +
            "m1.name AS area,\n" +
            "m.name as series,\n" +
            "sum(ifnull(ROUND(valid_length/coefficientInfo.coefficient,3),0)) as weight \n" +
            "from jinko_mes.production_painting_line ppl\n" +
            "LEFT JOIN jinko_mes.production_long_crystal plc ON ppl.crystal_no  = plc.crystal_no \n" +
            "LEFT JOIN jinko_mes.init_equipment ie ON plc.equipment_code=ie.code\n" +
            "LEFT JOIN jinko_mes.factory_model m ON ie.series=m.code\n" +
            "LEFT JOIN jinko_mes.factory_model m1 ON ie.area=m1.code\n" +
            "LEFT JOIN (\n" +
            "SELECT sdi.name as spec,sdi.`value` as coefficient FROM jinko_mes.sys_dictionary_item sdi LEFT JOIN\n" +
            "jinko_mes.sys_dictionary_theme sdt ON sdi.theme_id = sdt.id\n" +
            "where sdt.`value` = 'WeightRatio' and sdi.`enable` = 1\n" +
            ") coefficientInfo on plc.specs = coefficientInfo.spec\n" +
            "where \n" +
            "-- 防止8点半时间点查询范围为8点半至8点半，时间处理延后一分钟\n" +
            "start_time >= DATE_FORMAT(DATE_SUB(DATE_SUB( now(), INTERVAL 8 HOUR), INTERVAL 31 MINUTE), '%Y-%m-%d 08:30:00' )\n" +
            "and start_time< now()\n" +
            "GROUP BY m.name,ie.series", nativeQuery = true)
    List<Map<String, Object>> getCapacityInputData();
}