package com.usky.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.system.domain.HceArticle;
import com.usky.system.domain.HceCategory;
import com.usky.system.mapper.HceArticleMapper;
import com.usky.system.mapper.HceCategoryMapper;
import com.usky.system.service.HceArticleService;
import com.usky.system.service.vo.HceArticleVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 帮助中心_内容管理 服务实现类
 * </p>
 *
 * @author han
 * @since 2023-08-30
 */
@Service
public class HceArticleServiceImpl extends AbstractCrudService<HceArticleMapper, HceArticle> implements HceArticleService {
    @Resource
    private HceCategoryMapper hceCategoryMapper;

    @Override
    public CommonPage<HceArticle> page(HceArticleVO hceArticleVO){
        IPage<HceArticle> page = new Page<>(hceArticleVO.getPageNum(),hceArticleVO.getPageSize());

        LambdaQueryWrapper<HceArticle> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(hceArticleVO.getId() != null,HceArticle::getId,hceArticleVO.getId())
                .eq(hceArticleVO.getCategoryid() != null,HceArticle::getCategoryid,hceArticleVO.getCategoryid())
                .like(StringUtils.isNotBlank(hceArticleVO.getTitle()),HceArticle::getTitle,hceArticleVO.getTitle())
                .between(StringUtils.isNotBlank(hceArticleVO.getStartTime()) && StringUtils.isNotBlank(hceArticleVO.getEndTime()),HceArticle::getCreatedate,hceArticleVO.getStartTime(),hceArticleVO.getEndTime())
                .orderByDesc(HceArticle::getIstop).orderByAsc(HceArticle::getSortindex, HceArticle::getId);
        page = this.page(page,queryWrapper);
        if(page.getRecords().size() > 0){

            List<Integer> categoryIdList = new ArrayList<>();
            for (int i = 0; i < page.getRecords().size(); i++) {
                Integer categoryId = page.getRecords().get(i).getCategoryid();
                if(!categoryIdList.contains(categoryId)){
                    categoryIdList.add(categoryId);
                }
            }

            List<HceCategory> list = new ArrayList<>();
            if(CollectionUtils.isNotEmpty(categoryIdList)){
                list = hceCategoryMapper.getMenuNameList(categoryIdList);
            }

            if(CollectionUtils.isNotEmpty(list)){
                for(int i=0;i<page.getRecords().size();i++){
//                    page.getRecords().get(i).setContent(HtmlUtils.htmlUnescape(page.getRecords().get(i).getContent()));
                    for(int j=0;j<list.size();j++){
                        Integer categoryId1 = page.getRecords().get(i).getCategoryid();
                        Integer categoryId2 = list.get(j).getId();
                        if(categoryId1.equals(categoryId2)){
                            page.getRecords().get(i).setCategoryName(list.get(j).getCategoryName());
                        }
                    }
                }
            }

        }

        return new CommonPage<>(page.getRecords(),page.getTotal(),hceArticleVO.getPageSize(),hceArticleVO.getPageNum());
    }

    public void add(HceArticle hceArticle){

        LambdaQueryWrapper<HceArticle> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(HceArticle::getCategoryid,hceArticle.getCategoryid())
                .eq(HceArticle::getTitle,hceArticle.getTitle());
        List<HceArticle> list = this.list(queryWrapper);
        if(list.size() > 0){
            throw new BusinessException("同一个栏目下面内容标题重复，请重新填写内容标题");
        }

//        hceArticle.setContent(HtmlUtils.htmlEscapeHex(hceArticle.getContent()));
        hceArticle.setCreatedate(LocalDateTime.now());

        this.save(hceArticle);
    }

    public void update(HceArticle hceArticle){
        LambdaQueryWrapper<HceArticle> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(HceArticle::getCategoryid,hceArticle.getCategoryid())
                .eq(HceArticle::getTitle,hceArticle.getTitle())
                .ne(HceArticle::getId,hceArticle.getId());
        List<HceArticle> list = this.list(queryWrapper);
        if(list.size() > 0){
            throw new BusinessException("同一个栏目下面内容标题重复，请重新填写内容标题");
        }

//        hceArticle.setContent(HtmlUtils.htmlEscapeHex(hceArticle.getContent()));
        hceArticle.setModifydate(LocalDateTime.now());
        this.updateById(hceArticle);
    }

    public void remove(Integer id){
        this.removeById(id);

    }
}
