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

import com.github.pagehelper.PageHelper;
import com.hwrj.cloud.admin.dao.PmsBrandDao;
import com.hwrj.cloud.admin.dto.PmsBrandParam;
import com.hwrj.cloud.admin.dto.param.PmsBrandWebFrom;
import com.hwrj.cloud.admin.mapper.PmsBrandMapper;
import com.hwrj.cloud.admin.mapper.PmsProductMapper;
import com.hwrj.cloud.admin.model.PmsBrand;
import com.hwrj.cloud.admin.model.PmsBrandExample;
import com.hwrj.cloud.admin.model.PmsProduct;
import com.hwrj.cloud.admin.model.PmsProductExample;
import com.hwrj.cloud.admin.service.PmsBrandService;
import com.hwrj.cloud.admin.util.CurrUserUtil;
import com.hwrj.cloud.common.exception.GlobalException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 商品品牌Service实现类
 * Created by macro on 2018/4/26.
 */
@Service
public class PmsBrandServiceImpl implements PmsBrandService {
    @Autowired
    private PmsBrandMapper brandMapper;
    @Autowired
    private PmsProductMapper productMapper;

    @Autowired
    private PmsBrandDao pmsBrandDao;

    @Override
    public List<PmsBrand> listAllBrand() {
        return brandMapper.selectByExample(new PmsBrandExample());
    }

    @Override
    public int createBrand(PmsBrandParam pmsBrandParam) {
        PmsBrand pmsBrand = new PmsBrand();
        BeanUtils.copyProperties(pmsBrandParam, pmsBrand);
        //如果创建时首字母为空，取名称的第一个为首字母
        if (StringUtils.isEmpty(pmsBrand.getFirstLetter())) {
            pmsBrand.setFirstLetter(pmsBrand.getName().substring(0, 1));
        }
        long i = pmsBrandDao.insertCheck(pmsBrand.getName());
        if(i>0){
            throw new GlobalException("该品牌已存在,无法新增!");
        }
        pmsBrand.setCreateId((long) CurrUserUtil.getUserId());
        pmsBrand.setCreateTime(new Date());
        return brandMapper.insertSelective(pmsBrand);
    }

    @Override
    public int updateBrand(Long id, PmsBrandParam pmsBrandParam) {
        PmsBrand pmsBrand = new PmsBrand();
        BeanUtils.copyProperties(pmsBrandParam, pmsBrand);
        pmsBrand.setId(id);
        //如果创建时首字母为空，取名称的第一个为首字母
        if (StringUtils.isEmpty(pmsBrand.getFirstLetter())) {
            pmsBrand.setFirstLetter(pmsBrand.getName().substring(0, 1));
        }
        //更新品牌时要更新商品中的品牌名称
        PmsProduct product = new PmsProduct();
        product.setBrandName(pmsBrand.getName());
        PmsProductExample example = new PmsProductExample();
        example.createCriteria().andBrandIdEqualTo(id);
        productMapper.updateByExampleSelective(product,example);
        return brandMapper.updateByPrimaryKeySelective(pmsBrand);
    }

    @Override
    public int deleteBrand(Long id) {
        long i = pmsBrandDao.deleteCheck(Arrays.asList(id));
        if(i>0){
            throw new GlobalException("商品数据绑定改品牌，无法删除!");
        }
        return brandMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteBrand(List<Long> ids) {
        if(ids.size()>0){
            long i = pmsBrandDao.deleteCheck(ids);
            if(i>0){
                throw new GlobalException("商品数据绑定改品牌，无法删除!");
            }
            PmsBrandExample pmsBrandExample = new PmsBrandExample();
            pmsBrandExample.createCriteria().andIdIn(ids);
            return brandMapper.deleteByExample(pmsBrandExample);
        }else {
            throw new GlobalException("请选择你要删除的商品!");
        }
    }

    @Override
    public List<PmsBrand> listBrand(PmsBrandWebFrom pmsBrandWebFrom) {
        Long compId = CurrUserUtil.getCompIdInfo();

        if(StringUtils.isEmpty(compId)){
             compId = pmsBrandWebFrom.getCompId();
        }
        PageHelper.startPage(pmsBrandWebFrom.getPageNum(), pmsBrandWebFrom.getPageSize());
        PmsBrandExample pmsBrandExample = new PmsBrandExample();
        pmsBrandExample.setOrderByClause("create_time desc,sort asc");
        PmsBrandExample.Criteria criteria = pmsBrandExample.createCriteria();
        String keyword = pmsBrandWebFrom.getKeyword();
        if (!StringUtils.isEmpty(keyword)) {
            criteria.andNameLike("%" + keyword + "%");
        }
        if(!StringUtils.isEmpty(compId)){
            criteria.andCompIdEqualTo(compId);
        }
        return brandMapper.selectByExample(pmsBrandExample);
    }

    @Override
    public PmsBrand getBrand(Long id) {
        return brandMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateShowStatus(List<Long> ids, Integer showStatus) {
        PmsBrand pmsBrand = new PmsBrand();
        pmsBrand.setShowStatus(showStatus);
        PmsBrandExample pmsBrandExample = new PmsBrandExample();
        pmsBrandExample.createCriteria().andIdIn(ids);
        return brandMapper.updateByExampleSelective(pmsBrand, pmsBrandExample);
    }

    @Override
    public int updateFactoryStatus(List<Long> ids, Integer factoryStatus) {
        PmsBrand pmsBrand = new PmsBrand();
        pmsBrand.setFactoryStatus(factoryStatus);
        PmsBrandExample pmsBrandExample = new PmsBrandExample();
        pmsBrandExample.createCriteria().andIdIn(ids);
        return brandMapper.updateByExampleSelective(pmsBrand, pmsBrandExample);
    }
}
