package com.fs.his.service.impl;
import java.util.ArrayList;
import java.util.List;
import com.fs.common.BeanCopyUtils;
import com.fs.common.utils.DateUtils;
import com.fs.his.vo.FsStoreProductCategoryVO;
import com.fs.his.vo.FsStoreProductCategorysVO;
import com.fs.his.vo.OptionsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.his.mapper.FsStoreProductCategoryMapper;
import com.fs.his.domain.FsStoreProductCategory;
import com.fs.his.service.IFsStoreProductCategoryService;

/**
 * 商品分类Service业务层处理
 *
 * @author fs
 * @date 2023-06-08
 */
@Service
public class FsStoreProductCategoryServiceImpl implements IFsStoreProductCategoryService
{
    @Autowired
    private FsStoreProductCategoryMapper fsStoreProductCategoryMapper;

    /**
     * 查询商品分类
     *
     * @param cateId 商品分类主键
     * @return 商品分类
     */
    @Override
    public FsStoreProductCategory selectFsStoreProductCategoryByCateId(Long cateId)
    {
        return fsStoreProductCategoryMapper.selectFsStoreProductCategoryByCateId(cateId);
    }

    /**
     * 查询商品分类列表
     *
     * @param fsStoreProductCategory 商品分类
     * @return 商品分类
     */
    @Override
    public List<FsStoreProductCategory> selectFsStoreProductCategoryList(FsStoreProductCategory fsStoreProductCategory)
    {
        return fsStoreProductCategoryMapper.selectFsStoreProductCategoryList(fsStoreProductCategory);
    }
    @SuppressWarnings("all")
    @Override
    public List<FsStoreProductCategoryVO> selectFsStoreProductCategoryListVO(FsStoreProductCategory fsStoreProductCategory) {
        List<FsStoreProductCategoryVO> a = fsStoreProductCategoryMapper.selectFsStoreProductCategoryListVO(fsStoreProductCategory);

        ArrayList<FsStoreProductCategoryVO> v = new ArrayList<>();
        for (FsStoreProductCategoryVO fv : a) {
            if(fv.getPid()==0L){
                ArrayList<FsStoreProductCategory> sec = new ArrayList<>();
                for (FsStoreProductCategoryVO vv : a) {
                    if (vv.getPid().equals(fv.getCateId())){
                        FsStoreProductCategory copy = BeanCopyUtils.copy(vv, FsStoreProductCategory.class);
                        sec.add(copy);
                    }
                }
                fv.setChildren(sec);
                v.add(fv);
            }


        }
        return v;



    }
    @Override
    public List<FsStoreProductCategoryVO> getPidList() {
        FsStoreProductCategoryVO vo = new FsStoreProductCategoryVO();
        List<FsStoreProductCategory> pid = fsStoreProductCategoryMapper.getPid();
        vo.setCateName("顶级目录");
        vo.setCateId(0L);
        vo.setChildren(pid);
        ArrayList<FsStoreProductCategoryVO> list = new ArrayList<>();
        list.add(vo);
        return list;
    }
    /**
     * 新增商品分类
     *
     * @param fsStoreProductCategory 商品分类
     * @return 结果
     */
    @Override
    public int insertFsStoreProductCategory(FsStoreProductCategory fsStoreProductCategory)
    {
        fsStoreProductCategory.setCreateTime(DateUtils.getNowDate());
        return fsStoreProductCategoryMapper.insertFsStoreProductCategory(fsStoreProductCategory);
    }

    /**
     * 修改商品分类
     *
     * @param fsStoreProductCategory 商品分类
     * @return 结果
     */
    @Override
    public int updateFsStoreProductCategory(FsStoreProductCategory fsStoreProductCategory)
    {
        fsStoreProductCategory.setUpdateTime(DateUtils.getNowDate());
        return fsStoreProductCategoryMapper.updateFsStoreProductCategory(fsStoreProductCategory);
    }

    /**
     * 批量删除商品分类
     *
     * @param cateIds 需要删除的商品分类主键
     * @return 结果
     */
    @Override
    public int deleteFsStoreProductCategoryByCateIds(Long[] cateIds)
    {
        return fsStoreProductCategoryMapper.deleteFsStoreProductCategoryByCateIds(cateIds);
    }

    /**
     * 删除商品分类信息
     *
     * @param cateId 商品分类主键
     * @return 结果
     */
    @Override
    public int deleteFsStoreProductCategoryByCateId(Long cateId)
    {
        return fsStoreProductCategoryMapper.deleteFsStoreProductCategoryByCateId(cateId);
    }

    @Override
    public List<OptionsVO> selectFsStoreProductPidList() {
        return fsStoreProductCategoryMapper.selectFsStoreProductPidList();
    }

    @Override
    public List<OptionsVO> selectFsStoreProductCategoryOptionsVO() {
        return fsStoreProductCategoryMapper.selectFsStoreProductCategoryOptionsVO();
    }

    @Override
    public List<FsStoreProductCategorysVO> selectFsStoreProductCategorysVOList() {
        List<FsStoreProductCategorysVO> fsStoreProductCategories = fsStoreProductCategoryMapper.selectFsStoreProductCategorysVOList();

        ArrayList<FsStoreProductCategorysVO> v = new ArrayList<>();
        for (FsStoreProductCategorysVO fsStoreProductCategorysVo : fsStoreProductCategories) {
            if(fsStoreProductCategorysVo.getPid()==0){
                ArrayList<FsStoreProductCategory> sec = new ArrayList<>();
                for (FsStoreProductCategorysVO vv : fsStoreProductCategories) {
                    if (vv.getPid().equals(fsStoreProductCategorysVo.getCateId())){
                        FsStoreProductCategory copy = BeanCopyUtils.copy(vv, FsStoreProductCategory.class);
                        sec.add(copy);
                    }
                }
                fsStoreProductCategorysVo.setChildren(sec);
                v.add(fsStoreProductCategorysVo);
            }


        }
        return v;
    }


}
