package com.njcn.process.service.impl;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.poi.excel.ExcelUtil;
import com.njcn.process.mapper.SupvAlarmBackMapper;
import com.njcn.process.pojo.param.SupvAlarmBackParam;
import com.njcn.process.pojo.po.SupvAlarmBack;
import com.njcn.process.service.ISupvAlarmBackService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.system.api.DicDataFeignClient;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.po.Dept;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 技术监督预告警单反馈数据表 服务实现类
 * </p>
 *
 * @author wr
 * @since 2023-09-01
 */
@Service
@RequiredArgsConstructor
public class SupvAlarmBackServiceImpl extends ServiceImpl<SupvAlarmBackMapper, SupvAlarmBack> implements ISupvAlarmBackService {

    private final DeptFeignClient deptFeignClient;
    @Value("${gw.code}")
    private String code;
    @Override
    public boolean addAlarmBack(SupvAlarmBackParam.AlarmBackInsert param) {
        //获取省单位
        Dept data = deptFeignClient.getDeptByCode(code).getData();
        SupvAlarmBack supvAlarmBack = BeanUtil.copyProperties(param, SupvAlarmBack.class);
        supvAlarmBack.setProvinceId(data.getCode());
        supvAlarmBack.setProvinceName(data.getName());
        supvAlarmBack.setIsUploadHead(0);
        return this.save(supvAlarmBack);
    }

    @Override
    public boolean updateAlarmBack(SupvAlarmBackParam.AlarmBackUpdate param) {
        SupvAlarmBack supvAlarmBack = BeanUtil.copyProperties(param, SupvAlarmBack.class);
        SupvAlarmBack byId = this.getById(param.getAlarmBackId());
        if(1==byId.getIsUploadHead()){
            supvAlarmBack.setIsUploadHead(3);
        }
        return this.updateById(supvAlarmBack);
    }

    @Override
    public boolean delAlarmBack(List<String> ids) {
        LambdaQueryWrapper<SupvAlarmBack> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.ne(SupvAlarmBack::getIsUploadHead, 0).in(SupvAlarmBack::getWorkAlarmId, ids);
        int count = this.count(lambdaQueryWrapper);
        if (count > 0) {
            throw new BusinessException("请选择未上送国网的删除");
        }
        return this.removeByIds(ids);
    }

    @Override
    public Page<SupvAlarmBack> pageAlarmBack(SupvAlarmBackParam param) {
        List<String> deptIds=new ArrayList<>();
        if(StrUtil.isNotBlank(param.getOrgId())){
            deptIds = deptFeignClient.getDepSonSelfCodetByCode(param.getOrgId()).getData();
        }
        Page<SupvAlarmBack> page = this.baseMapper.pageAlarmBack(new Page<>(param.getPageNum(), param.getPageSize()),
                deptIds,param
        );
        return page;
    }

    @Override
    public void downAlarmBack() {
        List<SupvAlarmBack> list = this.baseMapper.selectAlarmBack();
        ExportParams exportParams = new ExportParams("预告警单反馈数据模板（带*字段均是必填,请严格按照模板标准填入数据）", "预告警单反馈数据信息");
        ExcelUtil.exportExcel(exportParams,"预告警单反馈数据信息.xlsx",SupvAlarmBack.class, list);
    }
}
