package com.sz.admin.monitor.service.impl;

import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.sz.admin.monitor.mapper.SubstationMapper;
import com.sz.admin.monitor.pojo.dto.substation.SubstationCreateDTO;
import com.sz.admin.monitor.pojo.dto.substation.SubstationImportDTO;
import com.sz.admin.monitor.pojo.dto.substation.SubstationListDTO;
import com.sz.admin.monitor.pojo.dto.substation.SubstationUpdateDTO;
import com.sz.admin.monitor.pojo.po.Substation;
import com.sz.admin.monitor.pojo.vo.substation.SubstationVO;
import com.sz.admin.monitor.service.SubstationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.query.QueryChain;
import com.sz.core.common.enums.CommonResponseEnum;
import com.sz.core.util.PageUtils;
import com.sz.core.util.BeanCopyUtils;
import com.sz.core.util.Utils;
import com.sz.core.common.entity.PageResult;
import com.sz.core.common.entity.SelectIdsDTO;
import java.io.Serializable;
import java.util.List;

import com.sz.core.common.entity.ImportExcelDTO;
import com.sz.excel.core.ExcelResult;
import java.io.OutputStream;
import jakarta.servlet.http.HttpServletResponse;
import com.sz.core.util.FileUtils;
import com.sz.excel.utils.ExcelUtils;
import lombok.SneakyThrows;
import com.sz.core.datascope.SimpleDataScopeHelper;


/**
 * <p>
 * 变电站和超高压信息表 服务实现类
 * </p>
 *
 * @author Lz
 * @since 2026-03-06
 */
@Service
@RequiredArgsConstructor
public class SubstationServiceImpl extends ServiceImpl<SubstationMapper, Substation> implements SubstationService {
    @Override
    public void create(SubstationCreateDTO dto){
        Substation rySubstation = BeanCopyUtils.copy(dto, Substation.class);
        long count;
        // 唯一性校验
        count = QueryChain.of(Substation.class).eq(Substation::getName, dto.getName()).count();
        CommonResponseEnum.EXISTS.message("name已存在").assertTrue(count > 0);
        save(rySubstation);
    }

    @Override
    public void update(SubstationUpdateDTO dto){
        Substation rySubstation = BeanCopyUtils.copy(dto, Substation.class);
        QueryWrapper wrapper;
        // id有效性校验
        wrapper = QueryWrapper.create()
            .eq(Substation::getId, dto.getId());
        CommonResponseEnum.INVALID_ID.assertTrue(count(wrapper) <= 0);

        // 唯一性校验
        long count;
        count = QueryChain.of(Substation.class).eq(Substation::getName, dto.getName()).ne(Substation::getId, dto.getId()).count();
        CommonResponseEnum.EXISTS.message("name已存在").assertTrue(count > 0);
        saveOrUpdate(rySubstation);
    }

    @Override
    public PageResult<SubstationVO> page(SubstationListDTO dto){
        try {
            SimpleDataScopeHelper.start(Substation.class);
            Page<SubstationVO> page = pageAs(PageUtils.getPage(dto), buildQueryWrapper(dto),  SubstationVO.class);
            return PageUtils.getPageResult(page);
        } finally {
            SimpleDataScopeHelper.clearDataScope();
        }
    }

    @Override
    public List<SubstationVO> list(SubstationListDTO dto){
        try {
            SimpleDataScopeHelper.start(Substation.class);
            return listAs(buildQueryWrapper(dto), SubstationVO.class);
        } finally {
            SimpleDataScopeHelper.clearDataScope();
        }
    }

    @Override
    public void remove(SelectIdsDTO dto){
        CommonResponseEnum.INVALID_ID.assertTrue(dto.getIds().isEmpty());
        removeByIds(dto.getIds());
    }

    @Override
    public SubstationVO detail(Object id){
        Substation rySubstation = getById((Serializable) id);
        CommonResponseEnum.INVALID_ID.assertNull(rySubstation);
        return BeanCopyUtils.copy(rySubstation, SubstationVO.class);
    }

    @SneakyThrows
    @Override
    public void importExcel(ImportExcelDTO dto) {
        ExcelResult<SubstationImportDTO> excelResult = ExcelUtils.importExcel(dto.getFile().getInputStream(), SubstationImportDTO.class, true);
        List<SubstationImportDTO> list = excelResult.getList();
        // 入库
        List<Substation> importList = BeanCopyUtils.copyList(list, Substation.class);
        saveBatch(importList);
        List<String> errorList = excelResult.getErrorList();
        String analysis = excelResult.getAnalysis();
        System.out.println(" analysis : " + analysis);
        System.out.println(" isCover : " + dto.getIsCover());
    }

    @SneakyThrows
    @Override
    public void exportExcel(SubstationListDTO dto, HttpServletResponse response) {
        List<SubstationVO> list = list(dto);
        String fileName = "变电站和超高压信息表模板";
        OutputStream os = FileUtils.getOutputStream(response, fileName + ".xlsx");
        ExcelUtils.exportExcel(list, "变电站和超高压信息表", SubstationVO.class, os);
    }

    private static QueryWrapper buildQueryWrapper(SubstationListDTO dto) {
        QueryWrapper wrapper = QueryWrapper.create().from(Substation.class);
        if (Utils.isNotNull(dto.getName())) {
            wrapper.like(Substation::getName, dto.getName());
        }
        if (Utils.isNotNull(dto.getRegionId())) {
            wrapper.eq(Substation::getRegionId, dto.getRegionId());
        }
        if (Utils.isNotNull(dto.getType())) {
            wrapper.eq(Substation::getType, dto.getType());
        }
        if (Utils.isNotNull(dto.getAddress())) {
            wrapper.eq(Substation::getAddress, dto.getAddress());
        }
        return wrapper;
    }
}