package com.mcwl.resource.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mcwl.common.constant.DictConstants;
import com.mcwl.common.core.domain.R;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.common.utils.StringUtils;
import com.mcwl.resource.domain.ModelProduct;
import com.mcwl.resource.domain.Report;
import com.mcwl.resource.domain.WorkFlow;
import com.mcwl.resource.domain.vo.PageVo;
import com.mcwl.resource.mapper.ModelMapper;
import com.mcwl.resource.mapper.ReportMapper;
import com.mcwl.resource.mapper.WorkFlowMapper;
import com.mcwl.resource.service.ReportService;
import com.mcwl.system.init.DictInit;
import com.mcwl.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 举报 业务实现层
 *
 * @author DaiZibo
 * @date 2025/1/18
 * @apiNote
 */

@Service
public class ReportServiceImpl implements ReportService {


    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private SysUserMapper sysUserMapper;


    @Autowired
    private WorkFlowMapper workFlowMapper;

    @Autowired
    private ModelMapper modelMapper;

    @Override
    public void addReport(Report report) {

        report.setCreateTime(new Date());
        report.setUserId(SecurityUtils.getUserId());
        reportMapper.insert(report);
    }

    @Override
    public R<Page<Report>> selectReport(PageVo pageVo) {


        Page<Report> page = new Page<>(pageVo.getPageNumber(), pageVo.getPageSize());

        //构造查询条件
        LambdaQueryWrapper<Report> reportLambdaQueryWrapper = new LambdaQueryWrapper<>();
        reportLambdaQueryWrapper.eq(Report::getDelFlag, 0);
        if (pageVo.getType() != null) {
            reportLambdaQueryWrapper.eq(Report::getStatus, 0);
        }

        Page<Report> reportPage = reportMapper.selectPage(page, reportLambdaQueryWrapper);
        if (StringUtils.isEmpty(reportPage.getRecords())){
            return R.ok(reportPage);
        }
        for (Report record : reportPage.getRecords()) {
            record.setNickName(sysUserMapper.selectUserById(record.getUserId()).getNickName());
            //翻译举报类型
            record.setTranslateType(DictInit.getDictValue(DictConstants.REPORT_LABEL,record.getReportType()+""));
        }
        return R.ok(reportPage);
    }

    @Override
    public void deleteReport(Long id) {

        Report report = new Report().builder().id(id)
                .delFlag(2).build();

        reportMapper.updateById(report);
    }

    @Override
    public void updateStatus(Long productId,Integer type,Integer status,String text) {

        //修改所有同类举报申请
        reportMapper.updateStatus(productId,type,status);
        if (status == 2){

            //修改状态回退
            if (type == 0){
                //模型
                ModelProduct modelProduct = ModelProduct.builder().id(productId)
                        .auditStatus(4)
                        .auditText(text)
                        .build();
                modelMapper.updateById(modelProduct);
            }else {
                //工作流
                WorkFlow workFlow = WorkFlow.builder().id(productId)
                        .auditStatus(4)
                        .auditText(text)
                        .build();
                workFlowMapper.updateById(workFlow);
            }
        }
    }
}
