package com.chinaweal.aiccs.aiccs.fraudreg.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaweal.aiccs.aiccs.abnormal.entity.EBaseinfo;
import com.chinaweal.aiccs.aiccs.abnormal.entity.vo.TSBizSpeListVO;
import com.chinaweal.aiccs.aiccs.abnormal.service.EBaseinfoService;
import com.chinaweal.aiccs.aiccs.abnormal.service.IGzaiccodeService;
import com.chinaweal.aiccs.aiccs.fraudreg.entity.FraudBatchBaseinfo;
import com.chinaweal.aiccs.aiccs.fraudreg.mapper.FraudBatchBaseinfoMapper;
import com.chinaweal.aiccs.aiccs.fraudreg.service.FraudBatchBaseinfoService;
import com.chinaweal.aiccs.aiccs.fraudreg.service.RevokeFraudBatchService;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.BaseServiceImpl;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.page.PageRequestDto;
import com.chinaweal.youfool.framework.springboot.rest.RestResult;
import com.chinaweal.youfool.framework.springboot.rest.ResultCode;
import com.chinaweal.youfool.framework.sso.util.SSOUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.OutputStream;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 批量撤销登记关联主体表 服务实现类
 * </p>
 *
 * @author sh
 * @since 2023-03-14
 */
@Service
public class FraudBatchBaseinfoServiceImpl extends BaseServiceImpl<FraudBatchBaseinfoMapper, FraudBatchBaseinfo> implements FraudBatchBaseinfoService {

    @Autowired
    private EBaseinfoService eBaseinfoService;
    @Lazy
    @Autowired
    private RevokeFraudBatchService revokeFraudBatchService;
    @Resource
    private IGzaiccodeService gzaiccodeService;

    @Override
    public Page<FraudBatchBaseinfo> queryByPage(PageRequestDto pageRequestDto) {
        Map<String, Object> customParamMap = pageRequestDto.getCustomParamMap();
        String uniscid = customParamMap.get("uniscid") != null ? customParamMap.get("uniscid").toString() : "";
        String isremove = customParamMap.get("isremove") != null ? customParamMap.get("isremove").toString() : "";
        LambdaQueryWrapper<FraudBatchBaseinfo> wrapper = new LambdaQueryWrapper();
        wrapper.eq(customParamMap.get("bizseq") != null, FraudBatchBaseinfo :: getBizseq, customParamMap.get("bizseq").toString())
                .eq(StringUtils.isNotBlank(uniscid), FraudBatchBaseinfo :: getUniscid, uniscid)
                .eq(StringUtils.isNotBlank(isremove), FraudBatchBaseinfo :: getIsremove, isremove);
        Page<FraudBatchBaseinfo> page = page(new Page<>(pageRequestDto.getCurPage(), pageRequestDto.getPageSize()), wrapper);
        page.getRecords().forEach(each -> {
            each.setRegorgCn(gzaiccodeService.getNameByCode(each.getRegorg()));
            each.setIsnb(each.getIsnb().equals("0") ? "是" : "否");
        });
        return page;
    }

    @Override
    public void remove(String ids, String reasons) {
        AICUser aicUser = SSOUtil.getUser();
        String [] idArray = ids.split(";");
        for (int i=0; i<idArray.length; i++) {
            String id = idArray[i];
            FraudBatchBaseinfo fraudBatchBaseinfo = new FraudBatchBaseinfo();
            fraudBatchBaseinfo.setId(id);
            fraudBatchBaseinfo.setIsremove("1");
            fraudBatchBaseinfo.setReasons(reasons);
            fraudBatchBaseinfo.setUpdateTime(LocalDateTime.now());
            fraudBatchBaseinfo.setRemoveHandler(aicUser.getName());
            updateById(fraudBatchBaseinfo);
        }
    }

    @Override
    public RestResult<?> add(FraudBatchBaseinfo fraudBatchBaseinfo) {
        LambdaQueryWrapper<EBaseinfo> wrapper = new LambdaQueryWrapper();
        wrapper.eq(StringUtils.isNotBlank(fraudBatchBaseinfo.getUniscid()), EBaseinfo::getUniscid, fraudBatchBaseinfo.getUniscid())
                .eq(StringUtils.isNotBlank(fraudBatchBaseinfo.getRegno()), EBaseinfo::getRegno, fraudBatchBaseinfo.getRegno())
                .eq(StringUtils.isNotBlank(fraudBatchBaseinfo.getEntname()), EBaseinfo::getEntname, fraudBatchBaseinfo.getEntname());
        if (wrapper.getExpression().getNormal().size() == 0) {
            return RestResult.error(ResultCode.PARAM_NOT_COMPLETE,"参数错误");
        }
        EBaseinfo eBaseinfo = eBaseinfoService.getOne(wrapper);
        if (eBaseinfo == null) {
            return RestResult.error(ResultCode.PARAM_NOT_COMPLETE,"找不到对应主体");
        }
        fraudBatchBaseinfo.setId(IdUtil.simpleUUID());
        fraudBatchBaseinfo.setPripid(eBaseinfo.getPripid());
        fraudBatchBaseinfo.setUniscid(eBaseinfo.getUniscid());
        fraudBatchBaseinfo.setRegno(eBaseinfo.getRegno());
        fraudBatchBaseinfo.setEntname(eBaseinfo.getEntname());
        fraudBatchBaseinfo.setLerep(eBaseinfo.getLerep());
        fraudBatchBaseinfo.setDom(eBaseinfo.getDom());
        fraudBatchBaseinfo.setRegorg(eBaseinfo.getRegorg());
        fraudBatchBaseinfo.setRegorgCn(eBaseinfo.getRegorgCn());
        fraudBatchBaseinfo.setIsremove("0");
        Boolean isnb = revokeFraudBatchService.checknb(eBaseinfo.getPripid());
        fraudBatchBaseinfo.setIsnb(isnb ? "0" : "1");
        Boolean rs = save(fraudBatchBaseinfo);
        return RestResult.ok(rs);
    }

    @Override
    public void exportExcel(PageRequestDto pageRequestDto, OutputStream outputStream) {
        Page<FraudBatchBaseinfo> page = queryByPage(pageRequestDto);
        List<FraudBatchBaseinfo> list = page.getRecords();
        ExcelWriter excelWriter = EasyExcel.write(outputStream).build();
        WriteSheet writeSheet1 = EasyExcel.writerSheet(0, "sheet1").head(FraudBatchBaseinfo.class).build();
        excelWriter.write(list, writeSheet1);
        excelWriter.finish();
    }
}
