package com.njcn.bpm.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.bpm.enums.BpmResponseEnum;
import com.njcn.bpm.mapper.BpmSignMapper;
import com.njcn.bpm.pojo.param.BpmSignParam;
import com.njcn.bpm.pojo.po.BpmSign;
import com.njcn.bpm.pojo.vo.BpmSignVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njcn.bpm.service.IBpmSignService;
import com.njcn.bpm.utils.BeanUtils;
import com.njcn.common.pojo.enums.common.DataStateEnum;
import com.njcn.common.pojo.exception.BusinessException;
import com.njcn.web.factory.PageFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * BPM 流程标识 服务实现类
 * </p>
 *
 * @author hongawen
 * @since 2024-05-13
 */
@Service
public class BpmSignServiceImpl extends ServiceImpl<BpmSignMapper, BpmSign> implements IBpmSignService {

    @Override
    public String createSign(BpmSignParam bpmSignParam) {
        //判断名称和key是否重复
        checkSignName(bpmSignParam, false);
        checkSignKey(bpmSignParam, false);
        BpmSign sign = BeanUtils.toBean(bpmSignParam, BpmSign.class);
        sign.setState(DataStateEnum.ENABLE.getCode());
        this.baseMapper.insert(sign);
        return sign.getId();
    }

    private void checkSignName(BpmSignParam bpmSignParam, boolean isExcludeSelf) {
        LambdaQueryWrapper<BpmSign> signLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //判断流程表单的名称
        signLambdaQueryWrapper
                .eq(BpmSign::getName, bpmSignParam.getName())
                .eq(BpmSign::getState, DataStateEnum.ENABLE.getCode());
        //更新的时候，需排除当前记录
        if (isExcludeSelf) {
            if (bpmSignParam instanceof BpmSignParam.BpmSignUpdateParam) {
                signLambdaQueryWrapper.ne(BpmSign::getId, ((BpmSignParam.BpmSignUpdateParam) bpmSignParam).getId());
            }
        }
        int nameCountByAccount = this.count(signLambdaQueryWrapper);
        //大于等于1个则表示重复
        if (nameCountByAccount >= 1) {
            throw new BusinessException(BpmResponseEnum.REPEAT_SIGN_NAME_FORM);
        }
    }
    private void checkSignKey(BpmSignParam bpmSignParam, boolean isExcludeSelf) {
        LambdaQueryWrapper<BpmSign> signLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //判断流程表单的名称
        signLambdaQueryWrapper
                .eq(BpmSign::getSignKey, bpmSignParam.getSignKey())
                .eq(BpmSign::getState, DataStateEnum.ENABLE.getCode());
        //更新的时候，需排除当前记录
        if (isExcludeSelf) {
            if (bpmSignParam instanceof BpmSignParam.BpmSignUpdateParam) {
                signLambdaQueryWrapper.ne(BpmSign::getId, ((BpmSignParam.BpmSignUpdateParam) bpmSignParam).getId());
            }
        }
        int nameCountByAccount = this.count(signLambdaQueryWrapper);
        //大于等于1个则表示重复
        if (nameCountByAccount >= 1) {
            throw new BusinessException(BpmResponseEnum.REPEAT_SIGN_KEY_FORM);
        }
    }



    @Override
    public void updateSign(BpmSignParam.BpmSignUpdateParam updateParam) {
        //判断名称和key是否重复
        checkSignName(updateParam, true);
        checkSignKey(updateParam, true);
        // 更新
        BpmSign bpmSign = BeanUtils.toBean(updateParam, BpmSign.class);
        this.baseMapper.updateById(bpmSign);

    }

    @Override
    public void deleteSign(List<String> ids) {
        this.lambdaUpdate().set(BpmSign::getState, DataStateEnum.DELETED.getCode())
                .in(BpmSign::getId, ids)
                .update();
    }

    @Override
    public Page<BpmSignVO> getSignPage(BpmSignParam.BpmSignQueryParam bpmSignQueryParam) {
        QueryWrapper<BpmSignVO> bpmSignVOQueryWrapper = new QueryWrapper<>();

        if (StrUtil.isNotBlank(bpmSignQueryParam.getName())) {
            bpmSignVOQueryWrapper.like("bpm_sign.name", bpmSignQueryParam.getName());
        }

        if (StrUtil.isNotBlank(bpmSignQueryParam.getSignKey())) {
            bpmSignVOQueryWrapper.like("bpm_sign.sign_key", bpmSignQueryParam.getSignKey());
        }
        bpmSignVOQueryWrapper.eq("bpm_sign.state", DataStateEnum.ENABLE.getCode());
        bpmSignVOQueryWrapper.orderByAsc("bpm_sign.sort");
        return this.baseMapper.page(new Page<>(PageFactory.getPageNum(bpmSignQueryParam), PageFactory.getPageSize(bpmSignQueryParam)), bpmSignVOQueryWrapper);

    }

    @Override
    public List<BpmSign> getSignSimpleList() {
        LambdaQueryWrapper<BpmSign> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(BpmSign::getId,BpmSign::getName,BpmSign::getViewPath,BpmSign::getSignKey)
                .eq(BpmSign::getState,DataStateEnum.ENABLE.getCode());
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }
}
