package com.lutao.cloud.consume.factory;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import com.lutao.cloud.common.core.kafka.MessageCommandEnum;
import com.lutao.cloud.common.core.util.MessageInfo;
import com.lutao.cloud.collect.vo.DataAddVO;
import com.lutao.cloud.consume.service.PlatformInfoService;
import com.lutao.cloud.consume.service.ProblemRectificationDetailService;
import com.lutao.cloud.consume.service.SecurityRiskAssessmentService;
import com.lutao.cloud.consume.service.SecurityRiskAssessmentSummaryService;
import com.lutao.entity.PlatformInfo;
import com.lutao.entity.ProblemRectificationDetail;
import com.lutao.entity.SecurityRiskAssessment;
import com.lutao.entity.SecurityRiskAssessmentSummary;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 安全风险评估策略
 *
 * @author yx
 * @date 2023/07/21
 */
@Component
@Slf4j
public class SecurityRiskAssessmentStrategy implements CommandStrategy<SecurityRiskAssessment> {

    @Resource
    private SecurityRiskAssessmentService securityRiskAssessmentService;

    @Resource
    private ProblemRectificationDetailService problemRectificationDetailService;

    @Resource
    private SecurityRiskAssessmentSummaryService securityRiskAssessmentSummaryService;

    @Resource
    private PlatformInfoService platformInfoService;

    private static SecurityRiskAssessmentStrategy securityRiskAssessmentStrategy;

    @PostConstruct
    public void init() {
        securityRiskAssessmentStrategy = this;
        securityRiskAssessmentStrategy.securityRiskAssessmentService = this.securityRiskAssessmentService;
        securityRiskAssessmentStrategy.problemRectificationDetailService = this.problemRectificationDetailService;
        securityRiskAssessmentStrategy.securityRiskAssessmentSummaryService = this.securityRiskAssessmentSummaryService;
        securityRiskAssessmentStrategy.platformInfoService = this.platformInfoService;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MessageInfo execute(String command, JsonNode data, DataAddVO dataAddVO) {

        if (MessageCommandEnum.MESSAGE_ADD.getCommand().equals(command)) {
            SecurityRiskAssessment securityRiskAssessment = JSONUtil.toBean(data.toString(), SecurityRiskAssessment.class);
            securityRiskAssessment.setAppId(dataAddVO.getAppId());
            log.info("SecurityRiskAssessmentStrategy 进行add操作 ......");
            //根据appid，平台编码 查询安全评估（自评是否存在）
            securityRiskAssessmentStrategy.securityRiskAssessmentService.saveOrUpdate(securityRiskAssessment,
                    Wrappers.lambdaQuery(SecurityRiskAssessment.class)
                            .eq(SecurityRiskAssessment::getAppId,securityRiskAssessment.getAppId())
                            .eq(SecurityRiskAssessment::getOriginId,securityRiskAssessment.getOriginId()));
            //更新汇总表
            String evaluationCode = securityRiskAssessment.getEvaluationCode();
            //取前四位获取年份
            String year = evaluationCode.substring(0, 4);
            LambdaQueryWrapper<SecurityRiskAssessmentSummary> eq = Wrappers.lambdaQuery(SecurityRiskAssessmentSummary.class)
                    .eq(SecurityRiskAssessmentSummary::getAppId, securityRiskAssessment.getAppId())
                    .eq(SecurityRiskAssessmentSummary::getPlatformCode, securityRiskAssessment.getPlatformCode())
                    .eq(SecurityRiskAssessmentSummary::getEvaluationType, securityRiskAssessment.getEvaluationType())
                    .eq(SecurityRiskAssessmentSummary::getEvaluationYear, year);
            SecurityRiskAssessmentSummary securityRiskAssessmentSummary = securityRiskAssessmentStrategy.securityRiskAssessmentSummaryService.getOne(eq);
            if (securityRiskAssessmentSummary != null) {
                //更新操作
                BeanUtil.copyProperties(securityRiskAssessment, securityRiskAssessmentSummary);
                securityRiskAssessmentStrategy.securityRiskAssessmentSummaryService.update(eq);
            }else {
                //新增
                SecurityRiskAssessmentSummary securityRiskAssessmentSummary1 = new SecurityRiskAssessmentSummary();
                BeanUtil.copyProperties(securityRiskAssessment, securityRiskAssessmentSummary1);
                PlatformInfo platformInfo = platformInfoService.getOne(Wrappers.lambdaQuery(PlatformInfo.class)
                        .eq(PlatformInfo::getPlatformCode, securityRiskAssessmentSummary1.getPlatformCode())
                        .eq(PlatformInfo::getAppId, securityRiskAssessmentSummary1.getAppId()));
                if (platformInfo != null){
                    securityRiskAssessmentSummary1.setPlatformType(String.valueOf(platformInfo.getPlatformType()));
                }
                securityRiskAssessmentStrategy.securityRiskAssessmentSummaryService.save(securityRiskAssessmentSummary1);
            }


        } else if (MessageCommandEnum.MESSAGE_DEL.getCommand().equals(command)) {
            SecurityRiskAssessment securityRiskAssessment = JSONUtil.toBean(data.toString(), SecurityRiskAssessment.class);
            securityRiskAssessment.setAppId(dataAddVO.getAppId());

            log.info("SecurityRiskAssessmentStrategy 进行del操作 .......");
            securityRiskAssessmentStrategy.securityRiskAssessmentService.remove(Wrappers.lambdaQuery(SecurityRiskAssessment.class)
                    .eq(SecurityRiskAssessment::getAppId, securityRiskAssessment.getAppId())
                    .eq(SecurityRiskAssessment::getOriginId, securityRiskAssessment.getOriginId()));

            //删除安全评估 同时删除安全评估下面的问题
            LambdaQueryWrapper<ProblemRectificationDetail> eq = Wrappers.lambdaQuery(ProblemRectificationDetail.class).eq(ProblemRectificationDetail::getSraId, securityRiskAssessment.getOriginId());
            problemRectificationDetailService.remove(eq);

        } else if (MessageCommandEnum.MESSAGE_upd.getCommand().equals(command)) {
            SecurityRiskAssessment securityRiskAssessment = JSONUtil.toBean(data.toString(), SecurityRiskAssessment.class);
            securityRiskAssessment.setAppId(dataAddVO.getAppId());

            log.info("SecurityRiskAssessmentStrategy 进行upd操作 .......");
            securityRiskAssessmentStrategy.securityRiskAssessmentService.update(securityRiskAssessment, Wrappers.lambdaQuery(SecurityRiskAssessment.class)
                    .eq(SecurityRiskAssessment::getAppId, securityRiskAssessment.getAppId())
                    .eq(SecurityRiskAssessment::getOriginId, securityRiskAssessment.getOriginId()));
            //更新汇总表
            String evaluationCode = securityRiskAssessment.getEvaluationCode();
            //取前四位获取年份
            String year = evaluationCode.substring(0, 4);
            LambdaQueryWrapper<SecurityRiskAssessmentSummary> eq = Wrappers.lambdaQuery(SecurityRiskAssessmentSummary.class)
                    .eq(SecurityRiskAssessmentSummary::getAppId, securityRiskAssessment.getAppId())
                    .eq(SecurityRiskAssessmentSummary::getPlatformCode, securityRiskAssessment.getPlatformCode())
                    .eq(SecurityRiskAssessmentSummary::getEvaluationType, securityRiskAssessment.getEvaluationType())
                    .eq(SecurityRiskAssessmentSummary::getEvaluationYear, year);
            SecurityRiskAssessmentSummary securityRiskAssessmentSummary = securityRiskAssessmentStrategy.securityRiskAssessmentSummaryService.getOne(eq);
            if (securityRiskAssessmentSummary != null) {
                //更新操作
                BeanUtil.copyProperties(securityRiskAssessment, securityRiskAssessmentSummary);
                securityRiskAssessmentStrategy.securityRiskAssessmentSummaryService.update(eq);
            }else {
                //新增
                SecurityRiskAssessmentSummary securityRiskAssessmentSummary1 = new SecurityRiskAssessmentSummary();
                BeanUtil.copyProperties(securityRiskAssessment, securityRiskAssessmentSummary1);
                PlatformInfo platformInfo = platformInfoService.getOne(Wrappers.lambdaQuery(PlatformInfo.class)
                        .eq(PlatformInfo::getPlatformCode, securityRiskAssessmentSummary1.getPlatformCode())
                        .eq(PlatformInfo::getAppId, securityRiskAssessmentSummary1.getAppId()));
                if (platformInfo != null){
                    securityRiskAssessmentSummary1.setPlatformType(String.valueOf(platformInfo.getPlatformType()));
                }
                securityRiskAssessmentStrategy.securityRiskAssessmentSummaryService.save(securityRiskAssessmentSummary1);
            }
        } else if (MessageCommandEnum.MESSAGE_FIND.getCommand().equals(command)) {

            List<String> idList = new ArrayList<>();
            for (JsonNode node : data) {
                String id = node.get("id").asText();
                idList.add(id);
            }

            log.info("SecurityRiskAssessmentStrategy 进行find操作 .......");
            return MessageInfo.ok(securityRiskAssessmentStrategy.securityRiskAssessmentService.list(
                    Wrappers.lambdaQuery(SecurityRiskAssessment.class).in(SecurityRiskAssessment::getOriginId, idList)
                            .eq(SecurityRiskAssessment::getAppId,dataAddVO.getAppId())
            ));
        }

        return MessageInfo.ok();
    }

    @Override
    public MessageInfo executeBatch(List<DataAddVO> dataAddVOS) {
        return null;
    }
}
