package com.huimv.receive.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.receive.common.token.TokenSign;
import com.huimv.receive.common.utils.Result;
import com.huimv.receive.common.utils.ResultUtil;
import com.huimv.receive.entity.BaseFlow;
import com.huimv.receive.mapper.BaseFlowMapper;
import com.huimv.receive.service.IBaseFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-03-06
 */
@Service
public class BaseFlowServiceImpl extends ServiceImpl<BaseFlowMapper, BaseFlow> implements IBaseFlowService {

    @Autowired
    private BaseFlowMapper baseFlowMapper;

    @Override
    public Result add(HttpServletRequest httpServletRequest, BaseFlow baseFlow) {
        Integer userId = TokenSign.getMemberIdByJwtToken(httpServletRequest);
        if (baseFlowMapper.selectCount(new QueryWrapper<BaseFlow>().lambda()
                .eq(BaseFlow::getFlowName,baseFlow.getFlowName())) > 0) {
            return new Result(10001,"流程已存在!",false);
        }
        baseFlow.setCreateUser(userId);
        baseFlow.setCreateTime(LocalDateTime.now());
        return ResultUtil.addResult(baseFlowMapper.insert(baseFlow));
    }

    @Override
    public Result delete(String id) {
        return ResultUtil.deleteResult(baseFlowMapper.deleteById(id));
    }
}
