package com.muyu.rule.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.enums.SystemYesNo;
import com.muyu.common.core.utils.StringUtils;
import com.muyu.common.system.domain.SysDept;
import com.muyu.rule.common.domain.RuleEngine;
import com.muyu.rule.common.domain.RuleRegion;
import com.muyu.rule.common.domain.req.EtlRuleListReq;
import com.muyu.rule.common.domain.resp.EtlRuleResp;
import com.muyu.rule.server.mapper.EtlRuleMapper;
import com.muyu.rule.server.service.EtlRuleService;
import com.muyu.rule.server.service.RuleEngineVersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author：张承志
 * @Package：com.muyu.etl.service.impl
 * @Project：cloud-etl
 * @name：EtlRuleServiceImpl
 * @Date：2024/8/21 14:22
 */
@Service
public class EtlRuleServiceImpl extends ServiceImpl<EtlRuleMapper, RuleEngine>  implements EtlRuleService {

    @Autowired
    private EtlRuleMapper etlRuleMapper;

    @Autowired
    private RuleEngineVersionService versionService;



    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public RuleEngine queryById(int id){
        return etlRuleMapper.selectById(id);
    }



    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(int id){
        int total = etlRuleMapper.deleteById(id);
        return total > 0;
    }

    @Override
    public List<EtlRuleResp> selectList(EtlRuleListReq req) {

        LambdaQueryWrapper<RuleEngine> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(StringUtils.isNotEmpty(req.getName()), RuleEngine::getName,req.getName());

        queryWrapper.eq(
          StringUtils.isNotEmpty(req.getStatus()), RuleEngine::getStatus,req.getStatus()
        );
        queryWrapper.eq(
                StringUtils.isNotEmpty(req.getOpen()), RuleEngine::getOpen,req.getOpen()
        );

        queryWrapper.eq(
                StringUtils.isNotEmpty(req.getCode()), RuleEngine::getCode,req.getCode()
        );

        List<RuleEngine> etlRuleList = this.list(queryWrapper);

        List<EtlRuleResp> respList = etlRuleList.stream()
                .map(etlRule -> EtlRuleResp.etlRuleRespBuilder(etlRule)).toList();


        return respList;
    }

    @Override
    public List<RuleRegion> findAllRuleRegion() {


        return etlRuleMapper.findAllRuleRegion();
    }

    @Override
        public EtlRuleResp selectRuleAndVersionById(Long id) {

        RuleEngine ruleEngine = etlRuleMapper.selectById(id);

        EtlRuleResp etlRuleResp = EtlRuleResp.etlRuleRespBuilder(ruleEngine);

        etlRuleResp.setVersionList(versionService.selectRuleEngineVersion(ruleEngine.getId()));



        return etlRuleResp;
    }

    @Override
    public void openEngine(Long id) {

this.updateStatus(id,"0");

    }

    @Override
    public void closeEngine(Long id) {

        this.updateStatus(id,"1");

    }

    @Override
    public void activate(Long id) {

        this.updateOpen(id,SystemYesNo.YES.getCode());

    }

    @Override
    public void disable(Long id) {
        this.updateOpen(id,SystemYesNo.NO.getCode());
    }

    /**
     * 修改状态
     * @param id
     * @param status
     */

    public void updateStatus(Long id, String status){


        UpdateWrapper<RuleEngine> updateWrapper = new UpdateWrapper<>();

        updateWrapper.eq("id",id).set("status",status);

    this.update(updateWrapper);

    }

    /**
     * 修改激活
     * @param id
     * @param open
     */
    public void updateOpen(Long id, String open){


        UpdateWrapper<RuleEngine> updateWrapper = new UpdateWrapper<>();

        updateWrapper.eq("id",id).set("open",open);

        this.update(updateWrapper);

    }
}
