package com.hwrj.cloud.portal.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.hwrj.cloud.common.util.StringUtil;
import com.hwrj.cloud.portal.dao.UmsCompanyInfoDao;
import com.hwrj.cloud.portal.domain.UmsCompanyInfoDto;
import com.hwrj.cloud.portal.mapper.UmsCompanyFileMapper;
import com.hwrj.cloud.portal.mapper.UmsCompanyInfoMapper;
import com.hwrj.cloud.portal.mapper.UmsCompanyShopMapper;
import com.hwrj.cloud.portal.model.*;
import com.hwrj.cloud.portal.service.UmsCompanyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
@Service
public class UmsCompanyInfoServiceImpl implements UmsCompanyInfoService {
    @Autowired
    private UmsCompanyInfoMapper umsCompanyInfoMapper;

    @Autowired
    private UmsCompanyFileMapper umsCompanyFileMapper;
    @Autowired
    private UmsCompanyShopMapper umsCompanyShopMapper;

    @Autowired
    private UmsCompanyInfoDao umsCompanyDao;


    @Override
    public List<UmsCompanyInfo> searchCompanyInfo(String key) {
        UmsCompanyInfoExample example = new UmsCompanyInfoExample();
        UmsCompanyInfoExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(key)){
            criteria.andCompNameLike("%"+key+"%");
        }
        List<UmsCompanyInfo> infos = umsCompanyInfoMapper.selectByExample(example);

        return infos;
    }

    @Override
    public List<UmsCompanyInfoDto> searchCompanyInfo(String key, Integer pageNo, Integer pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        UmsCompanyInfoExample example = new UmsCompanyInfoExample();
        UmsCompanyInfoExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(key)){
            criteria.andCompNameLike("%"+key+"%");
        }
        List<UmsCompanyInfoDto> dtos = umsCompanyDao.selectByExampleWithBLOBs(example);

        return dtos;
    }

    @Override
    public UmsCompanyInfoDto detailInfo(Long id) {
        if (id == null){
            throw  new RuntimeException("非法操作");
        }
        UmsCompanyInfoDto umsCompanyInfoDto = new UmsCompanyInfoDto();
        UmsCompanyInfo info = umsCompanyInfoMapper.selectByPrimaryKey(id);
        if(StringUtils.isEmpty(info)){
            throw  new RuntimeException("不是供应商,无法完成发布商品!");
        }
        Long compId = info.getId();
        BeanUtil.copyProperties(info,umsCompanyInfoDto);
        List<UmsCompanyFile> listFile = getCompanyFileByCompanyId(compId);
        List<UmsCompanyShop> shopId = getCompanyShopByCompanyId(compId);
        umsCompanyInfoDto.setFiles(listFile);
        umsCompanyInfoDto.setShops(shopId);
        return umsCompanyInfoDto;
    }

    @Override
    public List<UmsCompanyShop> getCompanyShopByCompanyId(Long compId) {
        UmsCompanyShopExample example = new UmsCompanyShopExample();
        UmsCompanyShopExample.Criteria criteria = example.createCriteria();
        criteria.andCompIdEqualTo(compId);
        criteria.andShopStatusEqualTo(0);
        return umsCompanyShopMapper.selectByExample(example);

    }

    @Override
    public List<UmsCompanyFile> getCompanyFileByCompanyId(Long compId) {
        UmsCompanyFileExample example = new UmsCompanyFileExample();
        UmsCompanyFileExample.Criteria criteria = example.createCriteria();
        criteria.andCompIdEqualTo(compId);
        criteria.andFileStatusEqualTo(0);
        List<UmsCompanyFile> umsCompanyFiles = umsCompanyFileMapper.selectByExample(example);
        return umsCompanyFiles;
    }
}
