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

import com.github.pagehelper.PageHelper;
import com.hwrj.cloud.admin.common.CmsSubjectProductRelationInput;
import com.hwrj.cloud.admin.dao.CmsSubjectProductRelationDao;

import com.hwrj.cloud.admin.mapper.CmsSubjectMapper;
import com.hwrj.cloud.admin.mapper.CmsSubjectProductRelationMapper;
import com.hwrj.cloud.admin.model.*;
import com.hwrj.cloud.admin.service.CmsSubjectService;
import com.hwrj.cloud.admin.service.SmsHomeRecommendSubjectService;
import com.hwrj.cloud.common.exception.ApiException;
import org.checkerframework.checker.units.qual.C;
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.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品专题Service实现类
 */
@Service
public class CmsSubjectServiceImpl implements CmsSubjectService {
    @Autowired
    private CmsSubjectMapper subjectMapper;

    @Autowired
    private CmsSubjectProductRelationDao subjectProductRelationDao;

    @Autowired
    private CmsSubjectProductRelationMapper subjectProductRelationMapper;

    @Autowired
    private SmsHomeRecommendSubjectService smsHomeRecommendSubjectService;


    @Override
    public List<CmsSubject> listAll() {
        CmsSubjectExample example = new CmsSubjectExample();
        CmsSubjectExample.Criteria criteria = example.createCriteria();
        criteria.andDelFlagEqualTo(0);
//        criteria.andShowStatusEqualTo(1);
        example.setOrderByClause("sort asc");
        return subjectMapper.selectByExample(example);
    }

    @Override
    public List<CmsSubject> list(String keyword, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        CmsSubjectExample example = new CmsSubjectExample();
        CmsSubjectExample.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(keyword)) {
            criteria.andTitleLike("%" + keyword + "%");
        }

        criteria.andDelFlagEqualTo(0);
        example.setOrderByClause("sort asc");
        return subjectMapper.selectByExample(example);
    }

    @Override
    public void relateAndInsertList(List<CmsSubjectProductRelationInput> productRelationInputs, Long productId) {
        if (productRelationInputs== null||productRelationInputs.size()<1){
            return;
        }
        List<CmsSubjectProductRelation> productRelationList = relationConvert(productRelationInputs, productId);
        subjectProductRelationDao.insertList(productRelationList);
    }



    @Override
    public void relateAndUpdateList(List<CmsSubjectProductRelationInput> productRelationInputs, Long productId) {
        if (productRelationInputs == null){
            return;
        }
        List<CmsSubjectProductRelation> productRelationList = relationConvert(productRelationInputs, productId);
        //关联专题
        CmsSubjectProductRelationExample subjectProductRelationExample = new CmsSubjectProductRelationExample();
        subjectProductRelationExample.createCriteria().andProductIdEqualTo(productId);
        subjectProductRelationMapper.deleteByExample(subjectProductRelationExample);
        if(productRelationInputs.size()!=0){
            subjectProductRelationDao.insertList(productRelationList);
        }
    }

    @Override
    public void relateAndDeleteByProductId(List<Long> ids) {
        if (ids ==null || ids.size()<1){
            return;
        }
        CmsSubjectProductRelationExample subjectProductRelationExample = new CmsSubjectProductRelationExample();
        subjectProductRelationExample.createCriteria().andProductIdIn(ids);
        subjectProductRelationMapper.deleteByExample(subjectProductRelationExample);
    }

    @Override
    public List<CmsSubjectProductRelationInput> relationByProductId(Long productId) {
        List<CmsSubjectProductRelation> productRelationList = subjectProductRelationDao.selectSubjectProductRelationByProductId(productId);
        List<CmsSubjectProductRelationInput> productRelationInputs = productRelationList.stream().map(p -> {
            CmsSubjectProductRelationInput cmsSubjectProductRelationInput = new CmsSubjectProductRelationInput();
            BeanUtils.copyProperties(p, cmsSubjectProductRelationInput);
            return cmsSubjectProductRelationInput;
        }).collect(Collectors.toList());
        return productRelationInputs;
    }

    @Override
    public List<CmsSubject> subjectByCategory(Long id) {
        if (id == null){
            return null;
        }
        CmsSubjectExample example = new CmsSubjectExample();
        CmsSubjectExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(id);
        criteria.andDelFlagEqualTo(0);
        return subjectMapper.selectByExample(example);
    }

    @Override
    public List<CmsSubjectProductRelation> subjectRelationProduct(Long id) {
        if (id== null){
            return null;
        }
        CmsSubjectProductRelationExample example = new CmsSubjectProductRelationExample();
        CmsSubjectProductRelationExample.Criteria criteria = example.createCriteria();
        criteria.andSubjectIdEqualTo(id);
        List<CmsSubjectProductRelation> relations = subjectProductRelationMapper.selectByExample(example);
        return relations;
    }

    @Override
    public int add(CmsSubject cmsSubject) {
        if (cmsSubject != null){
            cmsSubject.setCreateTime(new Date());
            cmsSubject.setDelFlag(0);
            /*
             * 设置关联数量
             */
            cmsSubject.setCollectCount(0);
            /*
            评论数量
             */
            cmsSubject.setCommentCount(0);
            /*
            设置产品数量
             */
            cmsSubject.setProductCount(0);
            /*
            设置阅读数量
             */
            cmsSubject.setReadCount(0);
            return subjectMapper.insert(cmsSubject);
        }
        return 0;
    }

    @Override
    public int update(CmsSubject cmsSubject) {
        if (cmsSubject != null&&cmsSubject.getId() != null){
           return subjectMapper.updateByPrimaryKeyWithBLOBs(cmsSubject);
        }
        return 0;
    }

    @Override
    public int delete(Long id) {
        if (id != null){
            /*
             判断这条主题是否首页在用
            */
            List<SmsHomeRecommendSubject> recommendSubjects = smsHomeRecommendSubjectService.homeSubjectByCmsSubject(id);
            if (recommendSubjects != null&&recommendSubjects.size()>0){
                throw  new ApiException("主题已经关联了主题推介");
            }
            /*
            判断是否有关联的商品
             */

            List<CmsSubjectProductRelation> relations = subjectRelationProduct(id);

            if (relations != null&&relations.size()>0){
                throw  new ApiException("主题关联了商品");
            }

            CmsSubject subject = new CmsSubject();
            subject.setDelFlag(1);
            subject.setId(id);
            return subjectMapper.updateByPrimaryKeyWithBLOBs(subject);

        }

        return 0;
    }

    @Override
    public CmsSubject info(Long id) {
        if (id != null){
            return subjectMapper.selectByPrimaryKey(id);
        }
        return null;
    }

    @Override
    public int show(Long id, Integer showStatus) {
        if (id==null||showStatus == null||showStatus<0||showStatus>1){
            throw new ApiException("错误操作");
        }
        CmsSubject subject = new CmsSubject();
        subject.setId(id);
        subject.setShowStatus(showStatus);
        return subjectMapper.updateByPrimaryKeyWithBLOBs(subject);
    }


    @Override
    public List<CmsSubject> all(String keyword) {
        CmsSubjectExample example = new CmsSubjectExample();
        CmsSubjectExample.Criteria criteria = example.createCriteria();
        criteria.andDelFlagEqualTo(0);
        if (!StringUtils.isEmpty(keyword)){
            criteria.andTitleLike("%"+keyword+"%");
        }
        example.setOrderByClause("sort asc");
        return subjectMapper.selectByExample(example);
    }


    public List<CmsSubjectProductRelation> relationConvert(List<CmsSubjectProductRelationInput> productRelationInputs, Long productId) {
        return productRelationInputs.stream().map(p -> {
            CmsSubjectProductRelation cmsSubjectProductRelation = new CmsSubjectProductRelation();
            BeanUtils.copyProperties(p, cmsSubjectProductRelation);
            cmsSubjectProductRelation.setProductId(productId);
            return cmsSubjectProductRelation;
        }).collect(Collectors.toList());
    }
}
