package com.bonus.sgzb.material.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.bonus.sgzb.base.api.domain.TmTask;
import com.bonus.sgzb.material.domain.BmNoticeInfo;
import com.bonus.sgzb.material.domain.PurchaseCheckDetails;
import com.bonus.sgzb.material.domain.PurchaseCheckInfo;
import com.bonus.sgzb.material.mapper.PurchaseCheckDetailsMapper;
import com.bonus.sgzb.material.mapper.PurchaseCheckInfoMapper;
import com.bonus.sgzb.material.mapper.TaskMapper;
import com.bonus.sgzb.material.service.IPurchaseCheckInfoService;
import com.bonus.sgzb.common.core.utils.DateUtils;
import com.bonus.sgzb.material.vo.NoticeInfoVO;
import com.bonus.sgzb.system.api.RemoteUserService;
import javafx.concurrent.Task;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 新购验收任务Service业务层处理
 * 
 * @author bonus
 * @date 2023-12-10
 */
@Service
public class PurchaseCheckInfoServiceImpl implements IPurchaseCheckInfoService
{
    @Resource
    private PurchaseCheckInfoMapper purchaseCheckInfoMapper;

    @Resource
    private TaskMapper taskMapper;

    @Resource
    private PurchaseCheckDetailsMapper purchaseCheckDetailsMapper;

    @Resource
    private RemoteUserService remoteUserService;

    /**
     * 查询新购验收任务
     * 
     * @param taskId 新购验收任务主键
     * @return 新购验收任务
     */
    @Override
    public PurchaseCheckInfo selectPurchaseCheckInfoByTaskId(Long taskId)
    {
        return purchaseCheckInfoMapper.selectPurchaseCheckInfoByTaskId(taskId);
    }

    /**
     * 查询新购验收任务列表
     * 
     * @param purchaseCheckInfo 新购验收任务
     * @return 新购验收任务
     */
    @Override
    public List<PurchaseCheckInfo> selectPurchaseCheckInfoList(PurchaseCheckInfo purchaseCheckInfo)
    {
        List<PurchaseCheckInfo> purchaseCheckInfos = purchaseCheckInfoMapper.selectPurchaseCheckInfoList(purchaseCheckInfo);
        for (PurchaseCheckInfo checkInfo : purchaseCheckInfos) {
            String typeName = purchaseCheckInfoMapper.selectTypeNameByTaskId(checkInfo.getTaskId());
            checkInfo.setPurchasingTypeName(typeName);
        }
        return purchaseCheckInfos;
    }

    /**
     * 新增新购验收任务
     * 
     * @param purchaseCheckInfo 新购验收任务
     * @return 结果
     */
    @Override
    @Transactional
    public int insertPurchaseCheckInfo(PurchaseCheckInfo purchaseCheckInfo)
    {
        TmTask task = new TmTask();
        // 暂定的状态字典表
        task.setTaskType(14);
        task.setTaskStatus(24);
        // 采购单号
        task.setCode(purchaseCodeRule());
        task.setCreateTime(DateUtils.getNowDate());
        task.setCompanyId(purchaseCheckInfo.getCompanyId());
        // 创建任务信息
        taskMapper.insertTmTask(task);
        purchaseCheckInfo.setTaskId(task.getTaskId());
        purchaseCheckInfo.setCreateTime(DateUtils.getNowDate());

        // 批量新增新购任务详情信息
        List<PurchaseCheckDetails> checkDetailsList = purchaseCheckInfo.getCheckDetailsList();
        if (checkDetailsList != null) {
            for (PurchaseCheckDetails purchaseCheckDetails : checkDetailsList) {
                purchaseCheckDetails.setTaskId(task.getTaskId());
                purchaseCheckDetails.setStatus(2);
            }
            purchaseCheckDetailsMapper.insertPurchaseCheckDetailsList(checkDetailsList);
        }
        // 新增任务信息
        return purchaseCheckInfoMapper.insertPurchaseCheckInfo(purchaseCheckInfo);
    }

    // 采购单号编码生成规则
    private String purchaseCodeRule() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date nowDate = DateUtils.getNowDate();
        String format = dateFormat.format(nowDate);
        int taskNum = taskMapper.selectTaskNumByMonth(nowDate) + 1;
        String code = "XG" + format + "-000" + taskNum;
        return code;
    }

    /**
     * 修改新购验收任务
     * 
     * @param purchaseCheckInfo 新购验收任务
     * @return 结果
     */
    @Override
    public int updatePurchaseCheckInfo(PurchaseCheckInfo purchaseCheckInfo)
    {
        purchaseCheckInfo.setUpdateTime(DateUtils.getNowDate());
        return purchaseCheckInfoMapper.updatePurchaseCheckInfo(purchaseCheckInfo);
    }

    /**
     * 批量删除新购验收任务
     * 
     * @param taskIds 需要删除的新购验收任务主键
     * @return 结果
     */
    @Override
    public int deletePurchaseCheckInfoByTaskIds(Long[] taskIds)
    {
        return purchaseCheckInfoMapper.deletePurchaseCheckInfoByTaskIds(taskIds);
    }

    /**
     * 删除新购验收任务信息
     * 
     * @param taskId 新购验收任务主键
     * @return 结果
     */
    @Override
    public int deletePurchaseCheckInfoByTaskId(Long taskId)
    {
        return purchaseCheckInfoMapper.deletePurchaseCheckInfoByTaskId(taskId);
    }

    /**
     * 验收通知
     * @param noticeInfoVO 验收通知内容
     * @return 结果
     */
    @Override
    public Boolean insertBmNoticeInfo(NoticeInfoVO noticeInfoVO) throws Exception {
        // 任务状态修改
        TmTask task = new TmTask();
        Long taskId = noticeInfoVO.getTaskId();
        if (taskId == null) {
            throw new Exception("任务taskId为空");
        }
        task.setTaskId(taskId);
        task.setTaskStatus(25);
        taskMapper.updateTmTask(task);
        // 修改任务详情状态
        purchaseCheckDetailsMapper.updateCheckDetailsByTaskId(taskId);

        String message = noticeInfoVO.getMessage();
        List<BmNoticeInfo> bmNoticeInfoList = noticeInfoVO.getBmNoticeInfoList();
        if (bmNoticeInfoList.size() <= 0) {
            throw new Exception("绑定用户为空");
        }
        Boolean send = false;
        for (BmNoticeInfo bmNoticeInfo : bmNoticeInfoList) {
            String phone = bmNoticeInfo.getPhone();
            // 短信通知
            send = remoteUserService.send(phone, message);
            purchaseCheckInfoMapper.insertBmNoticeInfo(bmNoticeInfo);
        }
        return send;
    }
}
