package cn.com.ctop.common.module.service.impl;

import cn.com.ctop.common.module.entity.MaterialInfo;
import cn.com.ctop.common.module.entity.MaterialTagInfo;
import cn.com.ctop.common.module.entity.TagInfo;
import cn.com.ctop.common.module.mapper.MaterialTagInfoMapper;
import cn.com.ctop.common.module.service.IMaterialInfoService;
import cn.com.ctop.common.module.service.IMaterialTagInfoService;
import cn.com.ctop.common.module.service.ITagInfoService;
import cn.com.ctop.common.module.utils.ResultMapUtils;
import cn.com.ctop.common.module.utils.StatusCode;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 素材标签信息
 * @author jeecg-boot
 * @date   2020-08-27
 * @version V1.0
 */
@Service
public class MaterialTagInfoServiceImpl extends ServiceImpl<MaterialTagInfoMapper, MaterialTagInfo> implements IMaterialTagInfoService {
    @Autowired
    private IMaterialInfoService materialInfoService;
    @Autowired
    private ITagInfoService tagInfoService;
    @Resource
    private MaterialTagInfoMapper materialTagInfoMapper;
    @Override
    public Map<String, Object> mark(JSONObject data, LoginUser user) {
        Map<String,Object>result = new HashMap<>();
        String code = data.getString("code");
        if(null==code||"".equals(code.trim())){
            ResultMapUtils.setResultMap(result, StatusCode.COMMON_PARAM_ERROR);
            return result;
        }
        MaterialInfo materialInfo = materialInfoService.getMaterialInfoByCode(code);
        if(null == materialInfo){
            ResultMapUtils.setResultMap(result, StatusCode.COMMON_PARAM_ERROR);
            return result;
        }
        JSONArray tagList = data.getJSONArray("tagList");
        if(null == tagList){
            ResultMapUtils.setResultMap(result, StatusCode.COMMON_PARAM_ERROR);
            return result;
        }
        //删除之前的标签信息
        this.deleteByCode(code);
        for(int i=0;i<tagList.size();i++){
            Long tagId = tagList.getLong(i);
            TagInfo tagInfo = tagInfoService.getById(tagId);
            MaterialTagInfo setTag= new MaterialTagInfo(code,tagInfo,user.getId());
            this.save(setTag);
        }
        ResultMapUtils.setResultMap(result,StatusCode.COMMON_SUCCESS);
        return result;
    }

    @Override
    public Map<String, Object> getTagDetail(MaterialTagInfo materialTagInfo) {
        Map<String,Object>result = new HashMap<>();
        String code = materialTagInfo.getCode();
        if(null == code||"".equals(code.trim())){
            ResultMapUtils.setResultMap(result,StatusCode.COMMON_PARAM_ERROR);
            return result;
        }
        List<MaterialTagInfo> modalityList = this.getByParams(code,1L);
        result.put("modalityTagList",modalityList);
        List<MaterialTagInfo> modList = this.getByParams(code,2L);
        result.put("modTagList",modList);
        List<MaterialTagInfo> contentList = this.getByParams(code,3L);
        result.put("contentTagList",contentList);
        List<MaterialTagInfo> senceList = this.getByParams(code,4L);
        result.put("senceTagList",senceList);
        ResultMapUtils.setResultMap(result,StatusCode.COMMON_SUCCESS);
        return result;
    }

    public List<MaterialTagInfo> getByParams(String code,Long categotyId){
        QueryWrapper<MaterialTagInfo>queryWrapper = new QueryWrapper<>();
        if(null!=code&&!"".equals(code.trim())){
            queryWrapper.eq("code",code);
        }
        if(null!=categotyId&&categotyId!=0){
            queryWrapper.eq("category_id",categotyId);
        }
        return this.list(queryWrapper);
    }

    @Override
    public void deleteByCode(String code) {
        QueryWrapper<MaterialTagInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("code",code);
        materialTagInfoMapper.delete(wrapper);
    }
}
