package cn.stylefeng.workflow.design.modular.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.workflow.design.modular.entity.FlowUserComment;
import cn.stylefeng.workflow.design.modular.mapper.FlowUserCommentMapper;
import cn.stylefeng.workflow.design.modular.pojo.request.FlowUserCommentRequest;
import cn.stylefeng.workflow.design.modular.service.FlowUserCommentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class FlowUserCommentServiceImpl extends ServiceImpl<FlowUserCommentMapper, FlowUserComment> implements FlowUserCommentService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateTotal(FlowUserCommentRequest flowUserCommentRequest) {

        Long userId = LoginContext.me().getLoginUser().getUserId();

        LambdaUpdateWrapper<FlowUserComment> deleteWrapper = new LambdaUpdateWrapper<>();
        deleteWrapper.eq(FlowUserComment::getUserId, userId);
        this.remove(deleteWrapper);

        List<FlowUserComment> flowUserCommentList = flowUserCommentRequest.getFlowUserCommentList();
        for (FlowUserComment flowUserComment : flowUserCommentList) {
            flowUserComment.setId(IdWorker.getId());
            flowUserComment.setUserId(userId);
        }
        this.saveBatch(flowUserCommentList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<FlowUserComment> findList(FlowUserCommentRequest flowUserCommentRequest) {
        LambdaQueryWrapper<FlowUserComment> wrapper = this.createWrapper(flowUserCommentRequest);

        wrapper.select(FlowUserComment::getId, FlowUserComment::getUserId, FlowUserComment::getCommentContent, FlowUserComment::getFldSort);

        List<FlowUserComment> flowUserCommentList = this.list(wrapper);

        if (ObjectUtil.isEmpty(flowUserCommentList)) {
            flowUserCommentList = this.createDefaultComment(LoginContext.me().getLoginUser().getUserId());
            this.saveBatch(flowUserCommentList);
        }

        return flowUserCommentList;
    }

    private LambdaQueryWrapper<FlowUserComment> createWrapper(FlowUserCommentRequest flowUserCommentRequest) {
        LambdaQueryWrapper<FlowUserComment> queryWrapper = new LambdaQueryWrapper<>();

        String searchText = flowUserCommentRequest.getSearchText();
        queryWrapper.like(ObjectUtil.isNotEmpty(searchText), FlowUserComment::getCommentContent, searchText);

        queryWrapper.eq(FlowUserComment::getUserId, LoginContext.me().getLoginUser().getUserId());

        queryWrapper.orderByAsc(FlowUserComment::getFldSort);

        return queryWrapper;
    }

    private List<FlowUserComment> createDefaultComment(Long userId) {
        List<FlowUserComment> defaultCommentList = new ArrayList<>();

        FlowUserComment flowUserComment = new FlowUserComment();
        flowUserComment.setId(IdWorker.getId());
        flowUserComment.setUserId(userId);
        flowUserComment.setCommentContent("同意");
        flowUserComment.setFldSort(new BigDecimal(100));
        defaultCommentList.add(flowUserComment);

        FlowUserComment flowUserComment2 = new FlowUserComment();
        flowUserComment2.setId(IdWorker.getId());
        flowUserComment2.setUserId(userId);
        flowUserComment2.setCommentContent("已阅");
        flowUserComment2.setFldSort(new BigDecimal(101));
        defaultCommentList.add(flowUserComment2);

        FlowUserComment flowUserComment3 = new FlowUserComment();
        flowUserComment3.setId(IdWorker.getId());
        flowUserComment3.setUserId(userId);
        flowUserComment3.setCommentContent("已完成");
        flowUserComment3.setFldSort(new BigDecimal(102));
        defaultCommentList.add(flowUserComment3);

        return defaultCommentList;
    }

}
