package com.jiayue.ssi.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiayue.ssi.backenum.ApproveOperaterEnum;
import com.jiayue.ssi.backenum.ApproveResultEnum;
import com.jiayue.ssi.backenum.ApproveStatusEnum;
import com.jiayue.ssi.constant.ApproveConstants;
import com.jiayue.ssi.entity.SysApprove;
import com.jiayue.ssi.entity.SysUser;
import com.jiayue.ssi.mapper.SysApproveMapper;
import com.jiayue.ssi.mapper.SysUserMapper;
import com.jiayue.ssi.service.SysApproveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
* 审批实现类
*
* @author xsl
* @since 2023/03/29
*/
@Service
public class SysApproveServiceImpl extends ServiceImpl<SysApproveMapper, SysApprove> implements SysApproveService {
    @Autowired
    SysUserMapper sysUserMapper;

    /**
     * 执行业务sql
     * @param sysApprove
     * @return
     */
    @Override
    @Transactional(propagation= Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean executeBizSql(SysApprove sysApprove) {
        // 通过的状态执行sql
        if (sysApprove.getModuleName().equals(ApproveConstants.MODULE_NAME_USER)){
            SysUser sysUser = JSONUtil.toBean(sysApprove.getParameterContent(),SysUser.class);
            if (sysApprove.getOperation().equals(String.valueOf(ApproveOperaterEnum.DELETE.getCode()))){
                // 用户管理删除操作
                LambdaUpdateWrapper<SysUser> updateWrapper = new UpdateWrapper<SysUser>().lambda();
                updateWrapper.eq(SysUser::getId, sysUser.getId()).set(SysUser::getDelFlag, 1);
                int count = sysUserMapper.update(new SysUser(), updateWrapper);
                if (count > 0) {
                    return true;
                }
            }
        }
        return false;
    }
}
