package cn.iocoder.yudao.module.system.service.worklog;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.controller.admin.worklog.vo.rule.LogRulePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.worklog.vo.rule.LogRuleSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.worklog.LogFormDO;
import cn.iocoder.yudao.module.system.dal.dataobject.worklog.LogRuleDO;
import cn.iocoder.yudao.module.system.dal.mysql.worklog.LogFormMapper;
import cn.iocoder.yudao.module.system.dal.mysql.worklog.LogRuleMapper;
import cn.iocoder.yudao.module.system.service.worklog.dto.LogUseSaveReqDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.LOG_RULE_NOT_EXISTS;

/**
 * 日志规则 Service 实现类
 *
 * @author 符溶馨
 */
@Service
@Validated
public class LogRuleServiceImpl implements LogRuleService {

    @Resource
    private LogRuleMapper logRuleMapper;

    @Resource
    private LogUseService logUseService;

    @Resource
    private LogFormMapper logFormMapper;

    @Override
    public Long createLogRule(LogRuleSaveReqVO createReqVO) {
        // 插入
        LogRuleDO logRule = BeanUtils.toBean(createReqVO, LogRuleDO.class);
        logRuleMapper.insert(logRule);

        //插入规则后，同步插入模板使用者
        List<LogUseSaveReqDTO> useDOs = createReqVO.getUserInfo().stream()
                .map(user -> {
                    LogUseSaveReqDTO logUseDo = new LogUseSaveReqDTO();
                    logUseDo.setRuleId(createReqVO.getId());
                    logUseDo.setFormId(createReqVO.getFormId());
                    logUseDo.setUseUserId(user.getUserId());
                    logUseDo.setUseUserDept(user.getDeptId());
                    return logUseDo;
                }).collect(Collectors.toList());
        logUseService.createLogUse(useDOs);

        //把规则编号更新至日志模板表中
        LogFormDO updateObj = new LogFormDO();
        updateObj.setId(logRule.getFormId());
        updateObj.setRuleId(logRule.getId());
        logFormMapper.updateById(updateObj);

        // 返回
        return logRule.getId();
    }

    @Override
    public void updateLogRule(LogRuleSaveReqVO updateReqVO) {
        // 校验存在
        validateLogRuleExists(updateReqVO.getId());
        // 更新
        LogRuleDO updateObj = BeanUtils.toBean(updateReqVO, LogRuleDO.class);
        logRuleMapper.updateById(updateObj);

        //更新规则表 同步更新模板使用者
        //插入规则后，同步插入模板使用者
        List<LogUseSaveReqDTO> useDOs = updateReqVO.getUserInfo().stream()
                .map(user -> {
                    LogUseSaveReqDTO logUseDo = new LogUseSaveReqDTO();
                    logUseDo.setRuleId(updateReqVO.getId());
                    logUseDo.setFormId(updateReqVO.getFormId());
                    logUseDo.setUseUserId(user.getUserId());
                    logUseDo.setUseUserDept(user.getDeptId());
                    return logUseDo;
                }).collect(Collectors.toList());
        logUseService.updateLogUse(useDOs, updateReqVO.getFormId());
    }

    @Override
    public void deleteLogRule(Long id) {
        // 校验存在
        validateLogRuleExists(id);
        // 删除
        logRuleMapper.deleteById(id);
    }

    @Override
    public void deleteLogRuleByFormId(Long formId) {

        if (logRuleMapper.selectCount(LogRuleDO::getFormId, formId) > 0L) {

            //删除规则
            logRuleMapper.delete(LogRuleDO::getFormId, formId);
        }
    }

    private void validateLogRuleExists(Long id) {
        if (logRuleMapper.selectById(id) == null) {
            throw exception(LOG_RULE_NOT_EXISTS);
        }
    }

    @Override
    public LogRuleDO getLogRule(Long formId) {
        return logRuleMapper.selectOne(LogRuleDO:: getFormId, formId);
    }

    @Override
    public PageResult<LogRuleDO> getLogRulePage(LogRulePageReqVO pageReqVO) {
        return logRuleMapper.selectPage(pageReqVO);
    }

    @Override
    public List<LogRuleDO> getLogRuleByIds(List<Long> ruleIds) {
        return logRuleMapper.selectBatchIds(ruleIds);
    }

    @Override
    public LogRuleDO getLogRuleByFormId(Long formId) {
        return logRuleMapper.selectOne(new LambdaQueryWrapper<LogRuleDO>()
                .eq(LogRuleDO::getFormId, formId));
    }

}