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

import cn.com.tenlion.configcontent.service.contentcomment.IContentCommentService;
import ink.wgink.app.AppTokenManager;
import ink.wgink.common.base.DefaultBaseService;
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.contentcommentlike.IContentCommentLikeDao;
import cn.com.tenlion.configcontent.pojo.dtos.contentcommentlike.ContentCommentLikeDTO;
import cn.com.tenlion.configcontent.pojo.vos.contentcommentlike.ContentCommentLikeVO;
import cn.com.tenlion.configcontent.pojo.bos.contentcommentlike.ContentCommentLikeBO;
import cn.com.tenlion.configcontent.pojo.pos.contentcommentlike.ContentCommentLikePO;
import cn.com.tenlion.configcontent.service.contentcommentlike.IContentCommentLikeService;
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: ContentCommentLikeServiceImpl
 * @Description: CBC内容管理评论点赞记录表
 * @Author: CodeFactory
 * @Date: 2021-08-02 10:31:46
 * @Version: 3.0
 **/
@Service
public class ContentCommentLikeServiceImpl extends DefaultBaseService implements IContentCommentLikeService {

    @Autowired
    private IContentCommentService iContentCommentService;

    @Autowired
    private IContentCommentLikeDao contentCommentLikeDao;

    @Override
    public void save(ContentCommentLikeVO contentCommentLikeVO) {
        saveReturnId(contentCommentLikeVO);
    }

    @Override
    public void save(String token, ContentCommentLikeVO contentCommentLikeVO) {
        saveReturnId(token, contentCommentLikeVO);
    }

    @Override
    public String saveReturnId(ContentCommentLikeVO contentCommentLikeVO) {
        return saveReturnId(null, contentCommentLikeVO);
    }

    @Override
    public String saveReturnId(String token, ContentCommentLikeVO contentCommentLikeVO) {
        /**
         * 查询当前的状态 , 如果是已经点赞则取消 , 如果没有点赞则保存
         */
        Map<String, Object> params = HashMapUtil.beanToMap(contentCommentLikeVO);
        if (token != null) {
            AppTokenUser appTokenUser = AppTokenManager.getInstance().getToken(token).getAppTokenUser();
            contentCommentLikeVO.setContentCommentLikeUserName(appTokenUser.getName());
            contentCommentLikeVO.setContentCommentLikeUserIcon(appTokenUser.getAvatar());
        } else {
            UserInfoBO userInfoBO = this.securityComponent.getCurrentUser();
            contentCommentLikeVO.setContentCommentLikeUserName(userInfoBO.getUserName());
        }
        if (StringUtils.isBlank(token)) {
            setSaveInfo(params);
        } else {
            setAppSaveInfo(token, params);
        }
        List<ContentCommentLikeDTO> list = list(params);
        if (list.size() > 0) {
            for(ContentCommentLikeDTO dto : list) {
                delete(dto.getContentCommentLikeId());
            }
            iContentCommentService.updateLike(contentCommentLikeVO.getContentCommentId(), -1);
            return "";
        }
        Map<String, Object> saveParams = HashMapUtil.beanToMap(contentCommentLikeVO);
        String contentCommentLikeId = UUIDUtil.getUUID();
        saveParams.put("contentCommentLikeId", contentCommentLikeId);
        if (StringUtils.isBlank(token)) {
            setSaveInfo(saveParams);
        } else {
            setAppSaveInfo(token, saveParams);
        }
        contentCommentLikeDao.save(saveParams);
        iContentCommentService.updateLike(contentCommentLikeVO.getContentCommentId(), +1);
        return contentCommentLikeId;
    }

    @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("contentCommentLikeIds", ids);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        contentCommentLikeDao.remove(params);
    }

    @Override
    public void delete(String id) {
        contentCommentLikeDao.delete(id);
    }

    @Override
    public void update(String contentCommentLikeId, ContentCommentLikeVO contentCommentLikeVO) {
        update(null, contentCommentLikeId, contentCommentLikeVO);
    }

    @Override
    public void update(String token, String contentCommentLikeId, ContentCommentLikeVO contentCommentLikeVO) {
        Map<String, Object> params = HashMapUtil.beanToMap(contentCommentLikeVO);
        params.put("contentCommentLikeId", contentCommentLikeId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        contentCommentLikeDao.update(params);
    }

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

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

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

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

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

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

    @Override
    public List<ContentCommentLikeDTO> list(Map<String, Object> params) {
        return contentCommentLikeDao.list(params);
    }

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

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

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

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

}