package com.fdage.service.impl;

import com.fdage.dao.base.TbExhibitionCollectionMapper;
import com.fdage.dao.base.TbExhibitionMapper;
import com.fdage.dao.base.TbExhibitionTypeMapper;
import com.fdage.dao.cust.TbCollectionMapperCust;
import com.fdage.dao.cust.TbExhibitionCollectionMapperCust;
import com.fdage.dao.cust.TbExhibitionMapperCust;
import com.fdage.pojo.TbExhibition;
import com.fdage.pojo.TbExhibitionCollection;
import com.fdage.pojo.TbExhibitionType;
import com.fdage.request.RequestExhibition;
import com.fdage.respon.ResponCollection;
import com.fdage.respon.ResponExhibition;
import com.fdage.service.IExhibitionService;
import com.fdage.util.HTMLSpirit;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by Hb_zzZ on 2019/9/12.
 */
@Service
public class ExhibitionServiceImpl implements IExhibitionService {

    @Autowired
    private TbExhibitionMapper mapper;

    @Autowired
    private TbExhibitionMapperCust mapperCust;

    @Autowired
    private TbExhibitionCollectionMapper collectionMapper;

    @Autowired
    private TbExhibitionCollectionMapperCust exhibitionCollectionMapperCust;

    @Autowired
    private TbCollectionMapperCust collectionMapperCust;


    @Override
    public int insertExhibitionCollection(TbExhibitionCollection bo) {
        return collectionMapper.insertSelective(bo);
    }

    @Override
    public int deleteExhibitionCollection(Long exhibitionId) {
        return exhibitionCollectionMapperCust.deleteByExhibitionId(exhibitionId);
    }

    @Override
    public List<ResponExhibition> findExhibitionCollection(Long exhibitionId) {
        return exhibitionCollectionMapperCust.findExhibitionCollection(exhibitionId);
    }

    @Override
    @Transactional
    public int insert(TbExhibition exhibition) {
//        if(exhibition.getState().intValue() == 0){
//            mapperCust.updateStateByEquipmentId(exhibition.getEquipmentId());
//        }
        return mapper.insertSelective(exhibition);
    }

    @Override
    @Transactional
    public int update(TbExhibition exhibition) {
//        if(exhibition.getState().intValue() == 0){
//            mapperCust.updateStateByEquipmentId(exhibition.getEquipmentId());
//        }
        return mapper.updateByPrimaryKeySelective(exhibition);
    }

    @Override
    public int delete(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public TbExhibition findById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<ResponExhibition> findCollectionByExhibition(Long exhibitionId) {
        return mapperCust.findCollectionByExhibition(exhibitionId);
    }

    @Override
    public List<ResponExhibition> findList(RequestExhibition bo) {
        if(bo == null){
            bo = new RequestExhibition();
            bo.setState(0);
            return mapperCust.findList(bo);
        }
        int pageNum = bo.getPageNum() == 0? 1: bo.getPageNum();
        int pageSize = bo.getPageSize() == 0? 10: bo.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        return mapperCust.findList(bo);
    }

    @Override
    public ResponExhibition getExhibitionByEquipmentId(Long id) {
        ResponExhibition responExhibition = mapperCust.getExhibitionByEquipmentId(id);
        if(responExhibition == null){
            return null;
        }
        //返回给文通的数据过滤掉html标签
        if(responExhibition.getDescription() != null){
            responExhibition.setDescription(HTMLSpirit.delHTMLTag(StringEscapeUtils.unescapeHtml4(responExhibition.getDescription())));
        }

        List<ResponCollection> collectionList = collectionMapperCust.findListByExhibitionId(responExhibition.getId());
        for(ResponCollection responCollection : collectionList){
            if(responCollection.getDescription() != null){
                responCollection.setDescription(HTMLSpirit.delHTMLTag(StringEscapeUtils.unescapeHtml4(responCollection.getDescription())));
            }
        }
        responExhibition.setCollectionList(collectionList);
        return responExhibition;
    }

    @Override
    public ResponExhibition getExhibitionById(Long id) {
        TbExhibition exhibition = mapper.selectByPrimaryKey(id);
        if(exhibition == null){
            return null;
        }
        ResponExhibition responExhibition = new ResponExhibition();
        BeanUtils.copyProperties(exhibition, responExhibition);

        responExhibition.setCollectionList(collectionMapperCust.findListByExhibitionId(responExhibition.getId()));
        return responExhibition;
    }

    @Override
    public List<TbExhibitionType> typeList() {
        return mapperCust.typeList();
    }

    @Override
    public int addOpenNum(Long id) {
        return mapperCust.addOpenNum(id);
    }

    @Override
    public Long exhibitionTotal() {
        return mapperCust.exhibitionTotal();
    }

    @Override
    public List<ResponExhibition> findListOrderBy() {
        return mapperCust.findListOrderBy();
    }
}
