package com.fs.oms.service.impl;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fs.common.utils.DateUtils;
import com.fs.oms.mapper.OmsSupplierProductMapper;
import com.fs.oms.vo.OmsPurchaseApplyStatusVO;
import com.fs.oms.vo.OmsPurchaseApplyVO;
import com.fs.oms.vo.OmsPurchaseProductVO;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.thoughtworks.xstream.core.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.oms.mapper.OmsPurchaseApplyMapper;
import com.fs.oms.domain.OmsPurchaseApply;
import com.fs.oms.service.IOmsPurchaseApplyService;

import javax.xml.crypto.Data;

/**
 * 采购申请单Service业务层处理
 *
 * @author fs
 * @date 2023-06-20
 */
@Service
public class OmsPurchaseApplyServiceImpl implements IOmsPurchaseApplyService
{
    @Autowired
    private OmsPurchaseApplyMapper omsPurchaseApplyMapper;

    @Autowired
    private OmsSupplierProductMapper omsSupplierProductMapper;

    /**
     * 查询采购申请单
     *
     * @param id 采购申请单ID
     * @return 采购申请单
     */
    @Override
    public OmsPurchaseApply selectOmsPurchaseApplyById(Long id)
    {
        return omsPurchaseApplyMapper.selectOmsPurchaseApplyById(id);
    }

    /**
     * 查询采购申请单列表
     *
     * @param omsPurchaseApply 采购申请单
     * @return 采购申请单
     */
    @Override
    public List<OmsPurchaseApplyVO> selectOmsPurchaseApplyList(OmsPurchaseApply omsPurchaseApply)
    {
        return omsPurchaseApplyMapper.selectOmsPurchaseApplyList(omsPurchaseApply);
    }

    /**
     * 新增采购申请单
     *
     * @param omsPurchaseApply 采购申请单
     * @return 结果
     */
    @Override
    public int insertOmsPurchaseApply(OmsPurchaseApply omsPurchaseApply)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String timeStr = sdf.format(new Date());
        String prefix = "PA";
        omsPurchaseApply.setCode(prefix + timeStr);
        omsPurchaseApply.setCreateTime(DateUtils.getNowDate());
        return omsPurchaseApplyMapper.insertOmsPurchaseApply(omsPurchaseApply);
    }

    /**
     * 修改采购申请单
     *
     * @param omsPurchaseApply 采购申请单
     * @return 结果
     */
    @Override
    public int updateOmsPurchaseApply(OmsPurchaseApply omsPurchaseApply)
    {
        omsPurchaseApply.setUpdateTime(DateUtils.getNowDate());
        return omsPurchaseApplyMapper.updateOmsPurchaseApply(omsPurchaseApply);
    }

    /**
     * 批量删除采购申请单
     *
     * @param ids 需要删除的采购申请单ID
     * @return 结果
     */
    @Override
    public int deleteOmsPurchaseApplyByIds(Long[] ids)
    {
        return omsPurchaseApplyMapper.deleteOmsPurchaseApplyByIds(ids);
    }

    /**
     * 删除采购申请单信息
     *
     * @param id 采购申请单ID
     * @return 结果
     */
    @Override
    public int deleteOmsPurchaseApplyById(Long id)
    {
        return omsPurchaseApplyMapper.deleteOmsPurchaseApplyById(id);
    }

    @Override
    public List<OmsPurchaseProductVO> selectOmsPurchaseProductList(OmsPurchaseProductVO omsPurchaseProductVO) {
        return omsSupplierProductMapper.selectPurchaseProduct(omsPurchaseProductVO);
    }

    @Override
    public List<OmsPurchaseProductVO> selectOmsPurchaseProductById(Long id) {
        String oms = omsSupplierProductMapper.selectOmsPurchaseProductById(id);
        Gson gson = new Gson();
        List<OmsPurchaseProductVO> list = gson.fromJson(oms, new TypeToken<List<Map<String, Object>>>(){}.getType());
        return list;
    }

    @Override
    public int updateApplyStatus(OmsPurchaseApplyStatusVO omsPurchaseApplyStatusVO) {
        omsPurchaseApplyStatusVO.setAuditTime(DateUtils.getNowDate());
        return omsPurchaseApplyMapper.updateApplyStatus(omsPurchaseApplyStatusVO);
    }
}
