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

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

import com.alibaba.fastjson.JSONObject;
import com.bonus.sgzb.material.domain.PurchaseMacodeInfo;
import com.bonus.sgzb.material.mapper.PurchaseMacodeInfoMapper;
import com.bonus.sgzb.material.service.IPurchaseMacodeInfoService;
import com.bonus.sgzb.common.core.utils.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 新购验收编号管理Service业务层处理
 * 
 * @author bonus
 * @date 2023-12-10
 */
@Service
public class PurchaseMacodeInfoServiceImpl implements IPurchaseMacodeInfoService
{
    @Resource
    private PurchaseMacodeInfoMapper purchaseMacodeInfoMapper;

    /**
     * 查询新购验收编号管理
     * 
     * @param taskId 新购验收编号管理主键
     * @return 新购验收编号管理
     */
    @Override
    public PurchaseMacodeInfo selectPurchaseMacodeInfoByTaskId(Long taskId)
    {
        return purchaseMacodeInfoMapper.selectPurchaseMacodeInfoByTaskId(taskId);
    }

    /**
     * 查询新购验收编号管理列表
     * 
     * @param purchaseMacodeInfo 新购验收编号管理
     * @return 新购验收编号管理
     */
    @Override
    public List<PurchaseMacodeInfo> selectPurchaseMacodeInfoList(PurchaseMacodeInfo purchaseMacodeInfo)
    {
        return purchaseMacodeInfoMapper.selectPurchaseMacodeInfoList(purchaseMacodeInfo);
    }

    /**
     * 新增新购验收编号管理
     * 
     * @param purchaseMacodeInfo 新购验收编号管理
     * @return 结果
     */
    @Override
    public int insertPurchaseMacodeInfo(PurchaseMacodeInfo purchaseMacodeInfo)
    {
        purchaseMacodeInfo.setCreateTime(DateUtils.getNowDate());
        return purchaseMacodeInfoMapper.insertPurchaseMacodeInfo(purchaseMacodeInfo);
    }

    // 编码规则
    private String codeRule() {
        String code = "NXJJ";

        return "1";
    }

    /**
     * 修改新购验收编号管理
     * 
     * @param purchaseMacodeInfo 新购验收编号管理
     * @return 结果
     */
    @Override
    public int updatePurchaseMacodeInfo(PurchaseMacodeInfo purchaseMacodeInfo)
    {
        purchaseMacodeInfo.setUpdateTime(DateUtils.getNowDate());
        return purchaseMacodeInfoMapper.updatePurchaseMacodeInfo(purchaseMacodeInfo);
    }

    /**
     * 批量删除新购验收编号管理
     * 
     * @param taskIds 需要删除的新购验收编号管理主键
     * @return 结果
     */
    @Override
    public int deletePurchaseMacodeInfoByTaskIds(Long[] taskIds)
    {
        return purchaseMacodeInfoMapper.deletePurchaseMacodeInfoByTaskIds(taskIds);
    }

    /**
     * 删除新购验收编号管理信息
     * 
     * @param taskId 新购验收编号管理主键
     * @return 结果
     */
    @Override
    public int deletePurchaseMacodeInfoByTaskId(Long taskId)
    {
        return purchaseMacodeInfoMapper.deletePurchaseMacodeInfoByTaskId(taskId);
    }

    /**
     * 生成设备编号
     * @param typeId 类型id
     * @return 编码结果
     */
    @Override
    public String generateEquipmentNumber(long typeId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyMM");
        Date nowDate = DateUtils.getNowDate();
        String format = sdf.format(nowDate);

        JSONObject jsonObject = purchaseMacodeInfoMapper.getTypeByTypeId(typeId);
        String typeCode = jsonObject.getString("typeCode");
        String specsCode = jsonObject.getString("specsCode");
        //序列号
        int count = purchaseMacodeInfoMapper.getSerialNumber(typeId,nowDate);
        // 编码规则
        String codingRule = "NXJJ" + typeCode + format + specsCode + "000" + count;
        return codingRule;
    }

}
