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

import com.sw.mes.yb.accessory.entity.EquipmentAccessory;
import com.sw.mes.core.base.BaseDao;
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 javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
* @Author
* @Description：
* @Date:
* @Modified By:
*/
public interface EquipmentAccessoryDao extends BaseDao<EquipmentAccessory, Integer> {

    @Query(value = "SELECT * FROM zhxx_equipment_accessory WHERE equipment_id=?1 and deleted = 0 ORDER BY warning_date ASC ", nativeQuery = true)
    List<EquipmentAccessory> getByEquipment_id(Long equipmentId);

    @Modifying
    @Transactional
    @Query(value = "UPDATE zhxx_equipment_accessory SET use_life=?2 WHERE material_id=?1 and deleted = 0", nativeQuery = true)
    void updateUseLife(Long materialId, Double useLife);

    @Query(value = "SELECT * FROM zhxx_equipment_accessory WHERE equipment_id=?1 AND material_id=?2 and deleted = 0", nativeQuery = true)
    List<EquipmentAccessory> findByEquipment_idAndMaterial_id(Long equipmentId, Long materialId);

    Integer countByGoodsCodeAndBindAndDeleted(String goodsCode, Integer bind, Integer deleted);

    @Query(value = "SELECT * FROM zhxx_equipment_accessory\n" +
            "WHERE goods_code = ?1\n" +
            "ORDER BY id DESC LIMIT 1", nativeQuery = true)
    EquipmentAccessory getUseTimeByCode(String code);

    @Query(value = "SELECT * FROM zhxx_equipment_accessory WHERE goods_code = ?1 and deleted = 0 ", nativeQuery = true)
    List<EquipmentAccessory> findByGoodCodes(String uniqueCode);

    @Query(value = "SELECT * from zhxx_equipment_accessory zea WHERE zea.device_no = ?1", nativeQuery = true)
    List<EquipmentAccessory> findByDeviceCode(String deviceNo);

    @Query(value = "SELECT  \n" +
            "\ta.id \n" +
            "\t,a.operate_date operateDate \n" +
            "\t,a.operate_user operateUser \n" +
            "\t,a.operate_user_id operateUserId \n" +
            "\t,a.newUseTime useTime \n" +
            "\t,a.museLife - a.newUseTime remainLife \n" +
            "\t,a.usedTime usedTime\n" +
            "\t,a.warning_date warningDate \n" +
            "\t,a.equipment_id equipmentId \n" +
            "\t,a.material_id materialId \n" +
            "\t,a.unit_name unitName \n" +
            "\t,a.goods_code goodsCode \n" +
            "\t,a.device_no deviceNo \n" +
            "\t,a.factory \n" +
            "\t,a.apply_date applyDate \n" +
            "\t,a.apply_user applyUser \n" +
            "\t,a.bind \n" +
            "\t,a.deleted \n" +
            "\t,a.num \n" +
            "\t,a.button_status buttonStatus \n" +
            "\t,a.threshold \n" +
            "\t,a.status \n" +
            "\t,a.area \n" +
            "\t,a.series \n" +
            "\t,a.mcode \n" +
            "\t,a.norms \n" +
            "\t,a.mType \n" +
            "\t,a.mwarning\n" +
            "\t,a.mwarnTime\n" +
            "\t,Round(a.museLife/24, 2) museLife\n" +
            "\t,a.mname\n" +
            "\t,a.apply_button applyButton\n" +
            "\t,a.batch_no batchNo\n" +
            "\t,a.engraving engraving\n" +
            "\t,a.carbon_felt carbonFelt\n" +
            "\t,a.accessory_type accessoryType\n" +
            "\t,a.remark remark\n" +
            "\tFROM  \n" +
            "\t( \n" +
            "\tSELECT   \n" +
            "\tzea1.*  \n" +
            "\t,(CASE  \n" +
            "\t\t\t WHEN zeds.`status` = 2 THEN ROUND(zea1.use_time, 2)  #设备状态为2 有效运行时间   \n" +
            "\t\t\t WHEN zeds.`status` = 1 THEN ROUND(zea1.use_time + ROUND(TIMESTAMPDIFF(SECOND , zeds.start_time, NOW())/60/60,2), 2) \n" +
            "\t\t\t ELSE ROUND(zea1.use_time, 2) #设备没有状态 有效运行时间 \n" +
            "\t\tEND) AS newUseTime\n" +
            "\t, ROUND(ifnull(zea1.used_time, 0) + TIMESTAMPDIFF(SECOND , zea1.operate_date, NOW())/60/60, 2) usedTime #已使用时间\n" +
            "\t, zeds.`status` #1为抽真空 2为停炉  \n" +
            "\t, ie.area #区域/模块  \n" +
            "\t, ie.series #系列  \n" +
            "\t, zam.codes mcode #料号  \n" +
            "\t, zam.norms #规格 \n" +
            "\t, fmt.`name` mType #物料类型 \n" +
            "\t, zam.warning mwarning #是否预警\n" +
            "\t, zam.warn_time mwarnTime #预警时间\n" +
            "\t, zam.use_life museLife #额定寿命\n" +
            "\t, zam.name mname #辅料名称\n" +
            "\t,CASE  \n" +
            "\tWHEN zeds.`status` = 2 THEN ROUND(zam.use_life - zam.warn_time - zea1.use_time, 2)  #设备状态为2 使用寿命-预警寿命-有效运行时间  阈值<=0则报警  \n" +
            "\tWHEN zeds.`status` = 1 THEN ROUND(zam.use_life - zam.warn_time - zea1.use_time - ROUND(TIMESTAMPDIFF(SECOND , zeds.start_time, NOW())/60/60,2), 2) \n" +
            "\t#设备状态为1 使用寿命-预警寿命-有效运行时间-(当前时间-设备开启抽真空时间)  阈值<=0则报警  \n" +
            "\tELSE ROUND(zam.use_life - zam.warn_time - zea1.use_time, 2) #设备没有状态 使用寿命-预警寿命-有效运行时间  阈值<=0则报警  \n" +
            "\tEND AS threshold \n" +
            "\tFROM zhxx_equipment_accessory zea1  \n" +
            "\tLEFT JOIN zhxx_equipment_device_status zeds ON zeds.equipment_id = zea1.equipment_id  \n" +
            "\tLEFT JOIN init_equipment ie ON ie.id = zea1.equipment_id  \n" +
            "\tLEFT JOIN zhxx_accessory_material zam ON zam.id = zea1.material_id  \n" +
            "\tLEFT JOIN factory_material_type fmt ON fmt.id = zam.model_id \n" +
            "\t) a \n" +
            "\tWHERE 1=1 AND a.deleted = 0 \n" +
            "\t\tAND IF(?1 is not NULL AND ?1 <> '', a.area = ?1, 1=1) \n" +
            "\tAND IF(?2 is not NULL AND ?2 <> '', a.series = ?2, 1=1) \n" +
            "\tAND IF(?3 is not NULL AND ?3 <> '', a.device_no LIKE ?3%, 1=1) \n" +
            "\tAND IF(?4 is not NULL AND ?4 <> '', a.mcode LIKE ?4%, 1=1) \n" +
            "\tAND IF(?5 is not NULL AND ?5 <> '', a.goods_code LIKE ?5%, 1=1) \n" +
            "\tAND IF(?6 is not NULL AND ?6 <> '', a.use_time = ?6, 1=1) \n" +
            "\tAND IF(?7 is not NULL AND ?7 <> '', a.factory LIKE ?7%, 1=1) \n" +
            "\tAND IF(?8 is not NULL AND ?8 <> '', a.operate_date >= ?8, 1=1) \n" +
            "\tAND IF(?9 is not NULL AND ?9 <> '', a.operate_date < ?9, 1=1) \n" +
            "\tAND IF(?10 is not NULL AND ?10 <> '', a.num = ?10, 1=1) \n" +
            "\tAND IF(?11 is not NULL AND ?11 <> '', a.batch_no = ?11, 1=1)"
            ,countQuery = "SELECT count(1) FROM zhxx_equipment_accessory zea1 " +
            " LEFT JOIN zhxx_equipment_device_status zeds ON zeds.equipment_id = zea1.equipment_id " +
            "LEFT JOIN init_equipment ie ON ie.id = zea1.equipment_id " +
            "LEFT JOIN zhxx_accessory_material zam ON zam.id = zea1.material_id " +
            "LEFT JOIN factory_material_type fmt ON fmt.id = zam.model_id " +
            "where deleted = 0 " +
            "AND IF(?1 is not NULL AND ?1 <> '', ie.area = ?1, 1=1) " +
            "AND IF(?2 is not NULL AND ?2 <> '', ie.series = ?2, 1=1) " +
            "AND IF(?3 is not NULL AND ?3 <> '', zea1.device_no LIKE ?3%, 1=1) " +
            "AND IF(?4 is not NULL AND ?4 <> '', zam.codes LIKE ?4%, 1=1) " +
            "AND IF(?5 is not NULL AND ?5 <> '', zea1.goods_code LIKE ?5%, 1=1) " +
            "AND IF(?6 is not NULL AND ?6 <> '', zea1.use_time = ?6, 1=1) " +
            "AND IF(?7 is not NULL AND ?7 <> '', zea1.factory LIKE ?7%, 1=1) " +
            "AND IF(?8 is not NULL AND ?8 <> '', zea1.operate_date >= ?8, 1=1) " +
            "AND IF(?9 is not NULL AND ?9 <> '', zea1.operate_date < ?9, 1=1) "+
            "AND IF(?10 is not NULL AND ?10 <> '', zea1.num = ?10, 1=1) " +
            "AND IF(?11 is not NULL AND ?11 <> '', zea1.batch_no = ?11, 1=1)"
    , nativeQuery = true)
    Page<Map<String, Object>> newListByPage(String area, String series
                                            , String deviceNo, String mcode
                                            , String goodsCode, Double useTime, String factory
                                            , Date startOperateDate, Date endOperateDate
                                            , String num, String batchNo, Pageable pageable);

    @Query(value = "SELECT      \n" +
            "\ta.id     \n" +
            "\t,a.operate_date operateDate     \n" +
            "\t,a.operate_user operateUser     \n" +
            "\t,a.operate_user_id operateUserId     \n" +
            "\t,a.newUseTime useTime   \n" +
            "\t,a.usedTime\n" +
            "\t,a.warning_date warningDate     \n" +
            "\t,a.equipment_id equipmentId     \n" +
            "\t,a.material_id materialId     \n" +
            "\t,a.unit_name unitName     \n" +
            "\t,a.goods_code goodsCode     \n" +
            "\t,a.device_no deviceNo     \n" +
            "\t,a.factory     \n" +
            "\t,a.apply_date applyDate     \n" +
            "\t,a.apply_user applyUser     \n" +
            "\t,a.bind     \n" +
            "\t,a.deleted     \n" +
            "\t,a.num     \n" +
            "\t,a.button_status buttonStatus     \n" +
            "\t,a.threshold     \n" +
            "\t,a.status     \n" +
            "\t,a.area     \n" +
            "\t,a.series     \n" +
            "\t,a.mcode     \n" +
            "\t,a.norms     \n" +
            "\t,a.mType     \n" +
            "\t,a.mwarning    \n" +
            "\t,a.mwarnTime    \n" +
            "\t,a.museLife    \n" +
            "\t,a.mname    \n" +
            "\t,a.apply_button applyButton    \n" +
            "\t,a.batch_no batchNo\n" +
            "\tFROM      \n" +
            "\t(     \n" +
            "\tSELECT       \n" +
            "\tzea1.*      \n" +
            "\t,(CASE      \n" +
            "\t\t\t WHEN zeds.`status` = 2 THEN ROUND(zea1.use_time, 2)  #设备状态为2 有效运行时间       \n" +
            "\t\t\t WHEN zeds.`status` = 1 THEN ROUND(zea1.use_time + ROUND(TIMESTAMPDIFF(SECOND , zeds.start_time, NOW())/60/60,2), 2)     \n" +
            "\t\t\t #设备状态为1 有效运行时间+(当前时间-设备开启抽真空时间)    \n" +
            "\t\t\t ELSE ROUND(zea1.use_time, 2) #设备没有状态 有效运行时间     \n" +
            "\t\tEND) AS newUseTime \n" +
            "\t, ROUND(ifnull(zea1.used_time,0) + TIMESTAMPDIFF(SECOND , zea1.operate_date, NOW())/60/60,2)\t usedTime #已使用时间\n" +
            "\t, zeds.`status` #1为抽真空 2为停炉      \n" +
            "\t, ie.area #区域/模块      \n" +
            "\t, ie.series #系列      \n" +
            "\t, zam.codes mcode #料号      \n" +
            "\t, zam.norms #规格     \n" +
            "\t, fmt.`name` mType #物料类型     \n" +
            "\t, zam.warning mwarning #是否预警    \n" +
            "\t, zam.warn_time mwarnTime #预警时间    \n" +
            "\t, zam.use_life museLife #额定寿命    \n" +
            "\t, zam.name mname #辅料名称    \n" +
            "\t,CASE      \n" +
            "\tWHEN zeds.`status` = 2 THEN ROUND(zam.use_life - zam.warn_time - zea1.use_time, 2)  #设备状态为2 使用寿命-预警寿命-有效运行时间  阈值<=0则报警      \n" +
            "\tWHEN zeds.`status` = 1 THEN ROUND(zam.use_life - zam.warn_time - zea1.use_time - ROUND(TIMESTAMPDIFF(SECOND , zeds.start_time, NOW())/60/60,2), 2)     \n" +
            "\t#设备状态为1 使用寿命-预警寿命-有效运行时间-(当前时间-设备开启抽真空时间)  阈值<=0则报警      \n" +
            "\tELSE ROUND(zam.use_life - zam.warn_time - zea1.use_time, 2) #设备没有状态 使用寿命-预警寿命-有效运行时间  阈值<=0则报警      \n" +
            "\tEND AS threshold     \n" +
            "\tFROM zhxx_equipment_accessory zea1      \n" +
            "\tLEFT JOIN zhxx_equipment_device_status zeds ON zeds.equipment_id = zea1.equipment_id      \n" +
            "\tLEFT JOIN init_equipment ie ON ie.id = zea1.equipment_id      \n" +
            "\tLEFT JOIN zhxx_accessory_material zam ON zam.id = zea1.material_id      \n" +
            "\tLEFT JOIN factory_material_type fmt ON fmt.id = zam.model_id     \n" +
            "\t) a     \n" +
            "\tWHERE 1=1 AND a.deleted = 0     \n" +
            "\t\tAND IF(?1 is not NULL AND ?1 <> '', a.id = ?1, 1=1)   ", nativeQuery = true)
    Map<String, Object> getDetail(Long id);

    @Query(value = "SELECT \n" +
            "\tCASE  \n" +
            "\t\tWHEN zeds.`status` = 2 THEN ROUND(zea.use_time, 2)  #设备状态为2 已使用时间   \n" +
            "\t\tWHEN zeds.`status` = 1 THEN ROUND(zea.use_time + ROUND(TIMESTAMPDIFF(SECOND , zeds.start_time, NOW())/60/60,2), 2) \n" +
            "\t\t#设备状态为1 已使用时间+(当前时间-设备开启抽真空时间)\n" +
            "\t\tELSE ROUND(zea.use_time, 2) #设备没有状态 已使用时间 \n" +
            "\tEND AS useTime \n" +
            "FROM zhxx_equipment_accessory zea\n" +
            "LEFT JOIN zhxx_equipment_device_status zeds ON zeds.equipment_id = zea.equipment_id\n" +
            "WHERE zea.id = ?1", nativeQuery = true)
    BigDecimal getUseTime(Long equipmentAccessoryId);

    @Query(value = "SELECT * FROM zhxx_equipment_accessory WHERE goods_code = ?1 and deleted = 0 and bind = 1", nativeQuery = true)
    List<EquipmentAccessory> findByGoodCodesWithBind(String uniqueCode);

    @Query(value = "SELECT  \n" +
            "\tcount(*)\n" +
            " FROM  \n" +
            " ( \n" +
            " SELECT   \n" +
            " zea1.*  \n" +
            " ,(CASE  \n" +
            "\t\t\tWHEN zeds.`status` = 2 THEN ROUND(zea1.use_time, 2)  #设备状态为2 已使用时间   \n" +
            "\t\t\tWHEN zeds.`status` = 1 THEN ROUND(zea1.use_time + ROUND(TIMESTAMPDIFF(SECOND , zeds.start_time, NOW())/60/60,2), 2) \n" +
            "\t\t\t#设备状态为1 已使用时间+(当前时间-设备开启抽真空时间)\n" +
            "\t\t\tELSE ROUND(zea1.use_time, 2) #设备没有状态 已使用时间 \n" +
            "\t END) AS newUseTime\n" +
            " , zeds.`status` #1为抽真空 2为停炉  \n" +
            " , ie.area #区域/模块  \n" +
            " , ie.series #系列  \n" +
            " , zam.codes mcode #料号  \n" +
            " , zam.norms #规格 \n" +
            " , fmt.`name` mType #物料类型 \n" +
            " , zam.warning mwarning #是否预警\n" +
            " , zam.warn_time mwarnTime #预警时间\n" +
            " , zam.use_life museLife #额定寿命\n" +
            " , zam.name mname #辅料名称\n" +
            " , zam.unit_price munitPrice #单价\n" +
            " ,CASE  \n" +
            " WHEN zeds.`status` = 2 THEN ROUND(zam.use_life - zam.warn_time - zea1.use_time, 2)  #设备状态为2 使用寿命-预警寿命-已使用时间  阈值<=0则报警  \n" +
            " WHEN zeds.`status` = 1 THEN ROUND(zam.use_life - zam.warn_time - zea1.use_time - ROUND(TIMESTAMPDIFF(SECOND , zeds.start_time, NOW())/60/60,2), 2) \n" +
            " #设备状态为1 使用寿命-预警寿命-已使用时间-(当前时间-设备开启抽真空时间)  阈值<=0则报警  \n" +
            " ELSE ROUND(zam.use_life - zam.warn_time - zea1.use_time, 2) #设备没有状态 使用寿命-预警寿命-已使用时间  阈值<=0则报警  \n" +
            " END AS threshold \n" +
            " FROM zhxx_equipment_accessory zea1  \n" +
            " LEFT JOIN zhxx_equipment_device_status zeds ON zeds.equipment_id = zea1.equipment_id  \n" +
            " LEFT JOIN init_equipment ie ON ie.id = zea1.equipment_id  \n" +
            " LEFT JOIN zhxx_accessory_material zam ON zam.id = zea1.material_id  \n" +
            " LEFT JOIN factory_material_type fmt ON fmt.id = zam.model_id \n" +
            " ) a \n" +
            " WHERE 1=1 AND a.deleted = 0 \n" +
            "\t AND IF(?1 is not NULL AND ?1 <> '', a.area = ?1, 1=1) \n" +
            " AND IF(?2 is not NULL AND ?2 <> '', a.series = ?2, 1=1) \n" +
            " AND IF(?3 is not NULL AND ?3 <> '', a.device_no LIKE ?3%, 1=1) \n" +
            " AND IF(?4 is not NULL AND ?4 <> '', a.mcode LIKE ?4%, 1=1) \n" +
            " AND IF(?5 is not NULL AND ?5 <> '', a.goods_code LIKE ?5%, 1=1) \n" +
            " AND IF(?6 is not NULL AND ?6 <> '', a.use_time = ?6, 1=1) \n" +
            " AND IF(?7 is not NULL AND ?7 <> '', a.factory LIKE ?7%, 1=1) \n" +
            " AND IF(?8 is not NULL AND ?8 <> '', a.operate_date >= ?8, 1=1) \n" +
            " AND IF(?9 is not NULL AND ?9 <> '', a.operate_date < ?9, 1=1) \n" +
            " AND IF(?10 is not NULL AND ?10 <> '', a.num = ?10, 1=1) \n" +
            " AND IF(?11 is not NULL AND ?11 <> '', a.batch_no = ?11, 1=1)",nativeQuery = true)
    Integer countNewListByPage(String area, String series, String deviceNo, String mcode, String goodsCode, Double useTime, String factory, Date startOperateDate, Date endOperateDate, String num, String batchNo);
}
