package cn.cecep.talroad.service.impl.analysis;

import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.core.web.page.PageData;
import cn.cecep.talroad.domain.analysis.SRaActionReportsAudit;
import cn.cecep.talroad.domain.analysis.SRaActionReportsAuditFactoryInfo;
import cn.cecep.talroad.domain.analysis.SRaActionReportsAuditRecord;
import cn.cecep.talroad.domain.analysis.SRaActionReportsAuditResult;
import cn.cecep.talroad.domain.dto.AuditRecordDTO;
import cn.cecep.talroad.domain.dto.RecordListDTO;
import cn.cecep.talroad.domain.vo.AuditRecordLIstVO;
import cn.cecep.talroad.mapper.analysis.SRaActionReportsAuditFactoryInfoMapper;
import cn.cecep.talroad.mapper.analysis.SRaActionReportsAuditMapper;
import cn.cecep.talroad.mapper.analysis.SRaActionReportsAuditRecordMapper;
import cn.cecep.talroad.service.analysis.ISRaActionReportsAuditRecordService;
import cn.cecep.talroad.service.analysis.ISRaActionReportsAuditResultService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 执行报告规范性审核记录Service业务层处理
 *
 * @author szhpt
 * @date 2023-03-28*/
@Service
public class SRaActionReportsAuditRecordServiceImpl extends ServiceImpl<SRaActionReportsAuditRecordMapper, SRaActionReportsAuditRecord> implements ISRaActionReportsAuditRecordService
{

    @Resource
    private SRaActionReportsAuditMapper sRaActionReportsAuditMapper;
    @Autowired
    private SRaActionReportsAuditRecordMapper sRaActionReportsAuditRecordMapper;

    @Resource
    private SRaActionReportsAuditFactoryInfoMapper sRaActionReportsAuditFactoryInfoMapper;
    @Resource
    private ISRaActionReportsAuditResultService actionReportsAuditResultService;

    @Autowired
    private DataSourceTransactionManager dataSourceTransactionManager;

    /**
     * 查询执行报告规范性审核记录列表
     *
     * @param sRaActionReportsAuditRecord 执行报告规范性审核记录
     * @return 执行报告规范性审核记录
     */
    @Override
    public List<SRaActionReportsAuditRecord> selectSRaActionReportsAuditRecordList(SRaActionReportsAuditRecord sRaActionReportsAuditRecord)
    {
        return sRaActionReportsAuditRecordMapper.selectSRaActionReportsAuditRecordList(sRaActionReportsAuditRecord);
    }

    /**
     * 新建审核记录查询未审核企业列表
     * @param recordListDTO
     * @return
     */
    @Override
    public AjaxResults<Page<AuditRecordLIstVO>> recordList(RecordListDTO recordListDTO) {
        Page page = new Page(recordListDTO.getPageNum(),recordListDTO.getPageSize());
        page = sRaActionReportsAuditRecordMapper.recordList(page,recordListDTO);
        return AjaxResults.success(page);
    }


    /**
     * 新增审核记录
     * @param auditRecordDTO
     * @return
     */
    @Override
    @Transactional
    public AjaxResults saveRecord(AuditRecordDTO auditRecordDTO) {
        if (auditRecordDTO.getRecordIdList() == null || auditRecordDTO.getRecordIdList().size() == 0) {
            return AjaxResults.error("未选中任何企业！");
        }
        SRaActionReportsAudit sRaActionReportsAudit = sRaActionReportsAuditMapper.selectById(auditRecordDTO.getReportId());
        if(sRaActionReportsAudit == null){
            return AjaxResults.error("没有这个报告！");
        }
        SRaActionReportsAuditRecord sRaActionReportsAuditRecord = new SRaActionReportsAuditRecord();
        sRaActionReportsAuditRecord.setReportId(sRaActionReportsAudit.getId());
        sRaActionReportsAuditRecord.setAuditStatus(0L);
        sRaActionReportsAuditRecord.setTotalEnterprises(sRaActionReportsAudit.getTotalEnterprises());
        sRaActionReportsAuditRecord.setSubmittedEnterprises(sRaActionReportsAudit.getSubmittedEnterprises());
        sRaActionReportsAuditRecord.setCreateTime(new Date());
        sRaActionReportsAuditRecord.setAuditTime(new Date());
        this.save(sRaActionReportsAuditRecord);

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("id",auditRecordDTO.getRecordIdList());
        List<SRaActionReportsAuditFactoryInfo> factoryInfoList = sRaActionReportsAuditFactoryInfoMapper.selectList(queryWrapper);
        List<SRaActionReportsAuditFactoryInfo> tempInfo = new ArrayList<>();
        if (factoryInfoList.size()>0) {
            List<SRaActionReportsAuditResult> reportsAuditResults = new ArrayList<>();
            for (SRaActionReportsAuditFactoryInfo sRaActionReportsAuditFactoryInfo : factoryInfoList) {
                if (sRaActionReportsAuditFactoryInfo.getAuditStatus().compareTo(1) == 0) {
                    //已审核的不做处理
                    continue;
                }
                SRaActionReportsAuditResult sRaActionReportsAuditResult = new SRaActionReportsAuditResult();
                sRaActionReportsAuditResult.setRecordId(sRaActionReportsAuditRecord.getId());
                sRaActionReportsAuditResult.setFactoryId(sRaActionReportsAuditFactoryInfo.getFactoryId());
                sRaActionReportsAuditResult.setReportTime(sRaActionReportsAudit.getReportTime());
                sRaActionReportsAuditResult.setCreateTime(new Date());
                sRaActionReportsAuditResult.setReportSubmissionTime(sRaActionReportsAuditFactoryInfo.getCommitTime());
                reportsAuditResults.add(sRaActionReportsAuditResult);
                //更新为已审核
                sRaActionReportsAuditFactoryInfo.setAuditStatus(1);
                tempInfo.add(sRaActionReportsAuditFactoryInfo);
            }
            if(reportsAuditResults.size()>0){
                actionReportsAuditResultService.saveBatch(reportsAuditResults);
            }
            if(tempInfo.size()>0){
                TransactionStatus transaction = dataSourceTransactionManager.getTransaction(new DefaultTransactionDefinition());
                for (SRaActionReportsAuditFactoryInfo sRaActionReportsAuditFactoryInfo : tempInfo) {
                    sRaActionReportsAuditFactoryInfoMapper.updateById(sRaActionReportsAuditFactoryInfo);
                }
                dataSourceTransactionManager.commit(transaction);
            }
        }
        return AjaxResults.success("新增成功");
    }

}
