package com.fs.his.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.fs.his.domain.FsStoreProduct;
import com.fs.his.domain.FsStoreProductAttrValue;
import com.fs.his.domain.FsStoreProductPackage;
import com.fs.his.dto.StoreOrderProductDTO;
import com.fs.his.dto.StorePackageProductDTO;
import com.fs.his.mapper.FsStoreProductAttrValueMapper;
import com.fs.his.mapper.FsStoreProductMapper;
import com.fs.his.mapper.FsStoreProductPackageMapper;
import com.fs.his.param.FsStoreProductPackageQueryParam;
import com.fs.his.service.IFsStoreProductPackageService;
import com.fs.his.vo.FsStoreProductPacketVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品组合套餐Service业务层处理
 *
 * @author fs
 * @date 2022-07-14
 */
@Service
public class FsStoreProductPackageServiceImpl implements IFsStoreProductPackageService
{
    @Autowired
    private FsStoreProductPackageMapper fsStoreProductPackageMapper;
    @Autowired
    private FsStoreProductAttrValueMapper fsStoreProductAttrValueMapper;
    @Autowired
    private FsStoreProductMapper fsStoreProductMapper;
    /**
     * 查询商品组合套餐
     *
     * @param packageId 商品组合套餐ID
     * @return 商品组合套餐
     */
    @Override
    public FsStoreProductPackage selectFsStoreProductPackageById(Long packageId)
    {
        return fsStoreProductPackageMapper.selectFsStoreProductPackageById(packageId);
    }

    /**
     * 查询商品组合套餐列表
     *
     * @param fsStoreProductPackage 商品组合套餐
     * @return 商品组合套餐
     */
    @Override
    public List<FsStoreProductPackage> selectFsStoreProductPackageList(FsStoreProductPackage fsStoreProductPackage)
    {
        return fsStoreProductPackageMapper.selectFsStoreProductPackageList(fsStoreProductPackage);
    }

    /**
     * 新增商品组合套餐
     *
     * @param fsStoreProductPackage 商品组合套餐
     * @return 结果
     */
    @Override
    public int insertFsStoreProductPackage(FsStoreProductPackage fsStoreProductPackage)
    {
        return fsStoreProductPackageMapper.insertFsStoreProductPackage(fsStoreProductPackage);
    }

    /**
     * 修改商品组合套餐
     *
     * @param fsStoreProductPackage 商品组合套餐
     * @return 结果
     */
    @Override
    public int updateFsStoreProductPackage(FsStoreProductPackage fsStoreProductPackage)
    {
        return fsStoreProductPackageMapper.updateFsStoreProductPackage(fsStoreProductPackage);
    }

    /**
     * 批量删除商品组合套餐
     *
     * @param packageIds 需要删除的商品组合套餐ID
     * @return 结果
     */
    @Override
    public int deleteFsStoreProductPackageByIds(Long[] packageIds)
    {
        return fsStoreProductPackageMapper.deleteFsStoreProductPackageByIds(packageIds);
    }

    /**
     * 删除商品组合套餐信息
     *
     * @param packageId 商品组合套餐ID
     * @return 结果
     */
    @Override

    public int deleteFsStoreProductPackageById(Long packageId)
    {
        return fsStoreProductPackageMapper.deleteFsStoreProductPackageById(packageId);
    }

    @Override
//    @DataScope(deptAlias = "p")
    public List<FsStoreProductPacketVO> selectFsStoreProductPackageListVO(FsStoreProductPackage fsStoreProductPackage) {
        List<FsStoreProductPacketVO> list=fsStoreProductPackageMapper.selectFsStoreProductPackageListVO(fsStoreProductPackage);

        return list;
    }

    @Override
    public List<FsStoreProductPacketVO> selectFsStoreProductPackageListQueryVO(FsStoreProductPackageQueryParam param) {
        List<FsStoreProductPacketVO> list= fsStoreProductPackageMapper.selectFsStoreProductPackageListQueryVO(param);
        for(FsStoreProductPacketVO vo:list){
            List<StoreOrderProductDTO> productList=new ArrayList<>();
            JSONArray jsonArray=JSONUtil.parseArray(vo.getProducts());
            List<StorePackageProductDTO> goodsList=JSONUtil.toList(jsonArray, StorePackageProductDTO.class);
            for(StorePackageProductDTO dto:goodsList){
                StoreOrderProductDTO productDTO=new StoreOrderProductDTO();
                FsStoreProductAttrValue attrValue=fsStoreProductAttrValueMapper.selectFsStoreProductAttrValueById(dto.getId());
                if(attrValue!=null){
                    FsStoreProduct product=fsStoreProductMapper.selectFsStoreProductByProductId(attrValue.getProductId());
                    if(product!=null){
                        productDTO.setProductId(product.getProductId());
                        productDTO.setId(dto.getId());
                        productDTO.setBarCode(attrValue.getBarCode());
                        productDTO.setPrice(attrValue.getPrice());
                        productDTO.setCount(dto.getCount());
                        productDTO.setSku(attrValue.getSku());
                        productDTO.setImage(attrValue.getImage());
                        productDTO.setProductName(product.getProductName());
                        productList.add(productDTO);
                    }

                }

            }
            vo.setProductList(productList);
        }
        return list;
    }

    @Override
    public int updateFsStoreProductPackages(Long[] orderIds, Long status) {
        return fsStoreProductPackageMapper.updateFsStoreProductPackages(orderIds,status);
    }

}
