package cn.com.tenlion.configcontent.service.contentlike.impl;

import cn.com.tenlion.configcontent.pojo.dtos.contentcomment.ContentCommentDTO;
import cn.com.tenlion.configcontent.pojo.dtos.contentcommentlike.ContentCommentLikeDTO;
import ink.wgink.app.AppTokenManager;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.module.wechat.pojo.pos.miniapp.MiniappUserPO;
import ink.wgink.module.wechat.service.miniapp.IMiniappUserService;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.app.AppTokenUser;
import ink.wgink.pojo.bos.UserInfoBO;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.map.HashMapUtil;
import ink.wgink.util.UUIDUtil;
import cn.com.tenlion.configcontent.dao.contentlike.IContentLikeDao;
import cn.com.tenlion.configcontent.pojo.dtos.contentlike.ContentLikeDTO;
import cn.com.tenlion.configcontent.pojo.vos.contentlike.ContentLikeVO;
import cn.com.tenlion.configcontent.pojo.bos.contentlike.ContentLikeBO;
import cn.com.tenlion.configcontent.pojo.pos.contentlike.ContentLikePO;
import cn.com.tenlion.configcontent.service.contentlike.IContentLikeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName: ContentLikeServiceImpl
 * @Description: CBC内容管理点赞记录表
 * @Author: CodeFactory
 * @Date: 2021-08-02 10:39:38
 * @Version: 3.0
 **/
@Service
public class ContentLikeServiceImpl extends DefaultBaseService implements IContentLikeService {

    @Autowired
    private IContentLikeDao contentLikeDao;

    @Autowired
    private IMiniappUserService iMiniappUserService;

    @Override
    public void save(ContentLikeVO contentLikeVO) {
        saveReturnId(contentLikeVO);
    }

    @Override
    public void save(String token, ContentLikeVO contentLikeVO) {
        saveReturnId(token, contentLikeVO);
    }

    @Override
    public String saveReturnId(ContentLikeVO contentLikeVO) {
        return saveReturnId(null, contentLikeVO);
    }

    @Override
    public String saveReturnId(String token, ContentLikeVO contentLikeVO) {
        /**
         * 查询当前的状态 , 如果是已经点赞则取消 , 如果没有点赞则保存
         */
        Map<String, Object> params = HashMapUtil.beanToMap(contentLikeVO);
        if (token != null) {
            AppTokenUser appTokenUser = AppTokenManager.getInstance().getToken(token).getAppTokenUser();
            contentLikeVO.setContentLikeUserName(appTokenUser.getName());
            contentLikeVO.setContentLikeUserIcon(appTokenUser.getAvatar());
        } else {
            UserInfoBO userInfoBO = this.securityComponent.getCurrentUser();
            contentLikeVO.setContentLikeUserName(userInfoBO.getUserName());
        }
        if (StringUtils.isBlank(token)) {
            setSaveInfo(params);
        } else {
            setAppSaveInfo(token, params);
        }
        List<ContentLikeDTO> list = list(params);
        if (list.size() > 0) {
            for(ContentLikeDTO dto : list) {
                delete(dto.getContentLikeId());
            }
            return "";
        }
        Map<String, Object> saveParams = HashMapUtil.beanToMap(contentLikeVO);
        String contentLikeId = UUIDUtil.getUUID();
        saveParams.put("contentLikeId", contentLikeId);
        if (StringUtils.isBlank(token)) {
            setSaveInfo(saveParams);
        } else {
            setAppSaveInfo(token, saveParams);
        }
        contentLikeDao.save(saveParams);
        return contentLikeId;
    }

    @Override
    public void remove(List<String> ids) {
        remove(null, ids);
    }

    @Override
    public void remove(String token, List<String> ids) {
        Map<String, Object> params = getHashMap(2);
        params.put("contentLikeIds", ids);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        contentLikeDao.remove(params);
    }

    @Override
    public void delete(String contentLikeId) {
        contentLikeDao.delete(contentLikeId);
    }

    @Override
    public void update(String contentLikeId, ContentLikeVO contentLikeVO) {
        update(null, contentLikeId, contentLikeVO);
    }

    @Override
    public void update(String token, String contentLikeId, ContentLikeVO contentLikeVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(contentLikeVO);
        params.put("contentLikeId", contentLikeId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        contentLikeDao.update(params);
    }

    @Override
    public ContentLikeDTO get(Map<String, Object> params) {
        return contentLikeDao.get(params);
    }

    @Override
    public ContentLikeDTO get(String contentLikeId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("contentLikeId", contentLikeId);
        return get(params);
    }

    @Override
    public ContentLikeBO getBO(Map<String, Object> params) {
        return contentLikeDao.getBO(params);
    }

    @Override
    public ContentLikeBO getBO(String contentLikeId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("contentLikeId", contentLikeId);
        return getBO(params);
    }

    @Override
    public ContentLikePO getPO(Map<String, Object> params) {
        return contentLikeDao.getPO(params);
    }

    @Override
    public ContentLikePO getPO(String contentLikeId) {
        Map<String, Object> params = super.getHashMap(2);
        params.put("contentLikeId", contentLikeId);
        return getPO(params);
    }

    @Override
    public List<ContentLikeDTO> list(Map<String, Object> params) {
        List<ContentLikeDTO> list = contentLikeDao.list(params);
        // 放入查看人员数据信息
        Set<String> userSet = new HashSet<String>();
        /**
         * 判断子节点的数量
         */
        Map<String, Object> query = getHashMap(5);
        query.putAll(params);
        for(ContentLikeDTO dto : list) {
            userSet.add(dto.getCreator());
        }
        // 查询出人员的信息
        List<String> userList = new ArrayList<String>();
        userList.addAll(userSet);
        List<MiniappUserPO> miniappUserPOS = iMiniappUserService.listPO(userList);
        Map<String, MiniappUserPO> userPOMap = new HashMap<String, MiniappUserPO>();
        for(MiniappUserPO userPO : miniappUserPOS) {
            userPOMap.put(userPO.getUserId(), userPO);
        }
        for(ContentLikeDTO dto : list) {
            MiniappUserPO userPO = userPOMap.get(dto.getCreator());
            if (userPO != null) {
                /**
                 * 封装人员头像信息, 昵称信息
                 */
                dto.setContentLikeUserName(userPO.getNickName());
                dto.setContentLikeUserIcon(userPO.getAvatarUrl());
            }
        }
        return list;
    }

    @Override
    public List<ContentLikeBO> listBO(Map<String, Object> params) {
        return contentLikeDao.listBO(params);
    }

    @Override
    public List<ContentLikePO> listPO(Map<String, Object> params) {
        return contentLikeDao.listPO(params);
    }

    @Override
    public SuccessResultList<List<ContentLikeDTO>> listPage(ListPage page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<ContentLikeDTO> contentLikeDTOs = list(page.getParams());
        PageInfo<ContentLikeDTO> pageInfo = new PageInfo<>(contentLikeDTOs);
        return new SuccessResultList<>(contentLikeDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public Integer count(Map<String, Object> params) {
        Integer count = contentLikeDao.count(params);
        return count == null ? 0 : count;
    }

}