package com.hfkj.service.impl;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.dao.CmsPatchMapper;
import com.hfkj.dao.SecDictionaryMapper;
import com.hfkj.entity.CmsPatch;
import com.hfkj.entity.CmsPatchExample;
import com.hfkj.entity.SecDictionary;
import com.hfkj.entity.SecDictionaryExample;
import com.hfkj.service.CmsPatchService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service(value = "cmsPatchService")
public class CmsPatchServiceImpl implements CmsPatchService {

    @Resource
    private CmsPatchMapper cmsPatchMapper;
    @Resource
    private SecDictionaryMapper secDictionaryMapper;

    @Override
    public int addPatch(CmsPatch cmsPatch) throws Exception {
        if (cmsPatch.getPatchType() == 3) {
            // 若视频附件，则patch_path存储封面图，video_path存储真实的路径
            cmsPatch.setVideoPath(cmsPatch.getPatchPath());
            cmsPatch.setPatchPath(getVideoFront());
        }

        return cmsPatchMapper.insert(cmsPatch);
    }

    @Override
    public int addListPatch(List<CmsPatch> patchList, Long contentId) throws Exception {
        String videoFront = getVideoFront();
        for(CmsPatch item : patchList) {
            if (item.getPatchType() == 3) {
                // 若视频附件，则patch_path存储封面图，video_path存储真实的路径
                item.setVideoPath(item.getPatchPath());
                item.setPatchPath(videoFront);
            }
        }

        return cmsPatchMapper.addListPatch(patchList, contentId);
    }

    @Override
    public int delPatch(Long id) throws Exception {
        CmsPatch patch = cmsPatchMapper.selectByPrimaryKey(id);
        if (patch == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.CMS_PATCH_NOT_FOUND, "");
        }

        return cmsPatchMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<CmsPatch> getListPatch(Map<String, String> paramsMap) throws Exception {
        CmsPatchExample example = new CmsPatchExample();
        CmsPatchExample.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotBlank(paramsMap.get("contentId"))) {
            criteria.andContentIdEqualTo(Long.valueOf(paramsMap.get("contentId")));
        }
        if (StringUtils.isNotBlank(paramsMap.get("patchName"))) {
            criteria.andPatchNameLike("%" +paramsMap.get("patchName")+ "%");
        }
        if (StringUtils.isNotBlank(paramsMap.get("patchType"))) {
            criteria.andPatchTypeEqualTo(Integer.valueOf(paramsMap.get("patchType")));
        }
        example.setOrderByClause("sort ASC, add_time DESC");

        return cmsPatchMapper.selectByExample(example);
    }

    @Override
    @Transactional
    public int delPatchesByContentId(Map<String, String> paramsMap) throws Exception {
        if (StringUtils.isBlank(paramsMap.get("contentId"))) {
            return 0;
        }

        CmsPatchExample example = new CmsPatchExample();
        CmsPatchExample.Criteria criteria = example.createCriteria();

        criteria.andContentIdEqualTo(Long.valueOf(paramsMap.get("contentId")));
        if (StringUtils.isNotBlank(paramsMap.get("patchType"))) {
            criteria.andPatchTypeEqualTo(Integer.valueOf(paramsMap.get("patchType")));
        }

        return cmsPatchMapper.deleteByExample(example);
    }

    /**
     *
     * @desc 在数据字典中查询 CMS视频附件的封面图
     */
    private String getVideoFront() throws Exception{
        SecDictionaryExample example = new SecDictionaryExample();
        SecDictionaryExample.Criteria criteria = example.createCriteria();

        criteria.andCodeTypeEqualTo("CMS_VIDEO_FRONT");
        criteria.andStatusEqualTo(1);
        example.setOrderByClause("sort_id ASC");

        List<SecDictionary> resultList = secDictionaryMapper.selectByExample(example);
        if (resultList != null && resultList.size() > 0) {
            return resultList.get(0).getCodeValue();
        }
        return null;
    }
}
