package com.sw.mes.issue.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.issue.dao.YbIssueRecordDao;
import com.sw.mes.issue.dto.FlsYbIssueRecordDto;
import com.sw.mes.issue.entity.YbIssueRecord;
import com.sw.mes.issue.service.YbIssueRecordService;
import org.springframework.util.CollectionUtils;

/**
 * @author wangjingyu
 * @datetime 2023年9月6日 下午4:47:26
 * @description
 */
@Service
public class YbIssueRecordServiceImpl extends BaseServiceImpl<YbIssueRecord> implements YbIssueRecordService {

    YbIssueRecordDao dao;

    @Autowired
    public void setDao(YbIssueRecordDao dao) {
        super.setBaseDao(dao);
        this.dao=dao;
    }

    @Transactional
    @Override
    public void deleteErrorRecord(String sectionNo) {
        List<YbIssueRecord> records = dao.findBySectionNo(sectionNo);
        for (Iterator<YbIssueRecord> iterator = records.iterator(); iterator.hasNext();) {
            YbIssueRecord record = iterator.next();
            record.setIsDeleted(1)
            .setGmtUpdate(new Date());
            dao.save(record);
        }
    }

    @Override
    public List<FlsYbIssueRecordDto> queryByTrayNo(String trayNo) {
        List<YbIssueRecord> records = dao.findByTrayNo(trayNo);
        List<FlsYbIssueRecordDto> dtos = new ArrayList<>(records.size());
        for (Iterator<YbIssueRecord> iterator = records.iterator(); iterator.hasNext();) {
            YbIssueRecord temp = iterator.next();
            FlsYbIssueRecordDto dto = new FlsYbIssueRecordDto();
            dto.setSectionNo(temp.getSectionNo());
            dto.setStatus(temp.getStatus());
            dto.setNumNo(temp.getNumNo());
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    public List<FlsYbIssueRecordDto> queryByBranchCode(String branchCode) {
        List<YbIssueRecord> records = dao.queryByBranchCodeLastFour(branchCode);
        records =  records.stream().sorted(Comparator.comparing(YbIssueRecord::getNumNo)).collect(Collectors.toList());
        List<FlsYbIssueRecordDto> dtos = new ArrayList<>(records.size());
        for (Iterator<YbIssueRecord> iterator = records.iterator(); iterator.hasNext();) {
            YbIssueRecord temp = iterator.next();
            FlsYbIssueRecordDto dto = new FlsYbIssueRecordDto();
            dto.setSectionNo(temp.getSectionNo());
            dto.setStatus(temp.getStatus());
            dto.setNumNo(temp.getNumNo());
            dtos.add(dto);
        }
        return dtos;
    }

    @Override
    @Transactional
    public Result sendBranchCodeToYbIssueRecord(String branchCode, Integer sendStatus) {
        List<YbIssueRecord> records = dao.queryByBranchCode(branchCode);
        if (CollectionUtils.isEmpty(records)) {
            return ResultGenerator.fail(branchCode + "不存在");
        } else {
            for (YbIssueRecord record : records) {
                record.setSendStatus(1);
                record.setGmtUpdate(new Date());
            }
            this.saveAll(records);
        }
        return ResultGenerator.success();
    }
}