package com.base.plan.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.base.plan.model.dto.request.QueryWindowListDTO;
import com.base.plan.model.entity.AlgSchemeStepWindow;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.List;

@Mapper
@Repository
public interface AlgSchemeStepWindowMapper extends BaseMapper<AlgSchemeStepWindow> {

    @Select("select * from alg_scheme_step_window order by created_date desc, parent_id")
    List<AlgSchemeStepWindow> listAll();

    @Insert("<script> " +
            "insert into alg_scheme_step_window(" +
            "id,scheme_step_id,window_failed_reason,created_date,created_by,last_modified_by," +
            "last_modified_date,satellite_id,satellite_code,start_time,end_time,parent_id,access_time,resolving_power,side_sway_value,cloth_with," +
            "coverage,pitch_angle,remark,remark_type,sun_angle" +
            ")values" +
            "<foreach collection='stepWindows' item='item' separator=','>" +
            "(#{item.id},#{item.schemeStepId},#{item.windowFailedReason},#{item.createdDate},#{item.createdBy}," +
            "#{item.lastModifiedBy},#{item.lastModifiedDate},#{item.satelliteId},#{item.satelliteCode},#{item.startTime},#{item.endTime}," +
            "#{item.parentId},#{item.accessTime},#{item.resolvingPower},#{item.sideSwayValue},#{item.clothWith}," +
            "#{item.coverage},#{item.pitchAngle},#{item.remark},#{item.remarkType},#{item.sunAngle})" +
            "</foreach>" +
            "</script>"
    )
    void insertBatch(@Param("stepWindows") List<AlgSchemeStepWindow> stepWindows);

    default List<AlgSchemeStepWindow> getWindowList(QueryWindowListDTO queryWindowListDTO) {
        QueryWrapper<AlgSchemeStepWindow> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(queryWindowListDTO.getStepId())) {
            queryWrapper.eq("scheme_step_id", queryWindowListDTO.getStepId());
        }
        if (!StringUtils.isEmpty(queryWindowListDTO.getParentId())) {
            queryWrapper.eq("parent_id", queryWindowListDTO.getParentId());
        }
        return this.selectList(queryWrapper.eq("remark_type", 0));
    }

    default List<AlgSchemeStepWindow> selectByParentIdAndSatellileId(String parentId, String satelliteId) {
        return this.selectList(new QueryWrapper<AlgSchemeStepWindow>().eq("parent_id", parentId).eq("remark_type", 0).eq("satellite_id", satelliteId));
    }

    @Update("<script>" +
            "update alg_scheme_step_window set scheme_step_id = #{stepId} " +
            "where id in " +
            "<foreach collection='windowIds' separator=',' item='item' open='(' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    void setStepIdByWindowId(@Param("windowIds") Collection<String> windowIds, @Param("stepId") String stepId);

    List<AlgSchemeStepWindow> getAlgSchemeStepWindowByParentIdSubstring(@Param("parentId") String parentIdSubstring);

    List<AlgSchemeStepWindow> getAlgSchemeStepWindowByParentIds(@Param("parentIds") List<String> parentIds);
}
