package com.gic.haoban.manage.service.service.hm.impl;

import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.Page;
import com.gic.commons.util.UniqueIdUtils;
import com.gic.haoban.common.utils.EntityUtil;
import com.gic.haoban.common.utils.PageUtil;
import com.gic.haoban.manage.api.dto.hm.HmPageDTO;
import com.gic.haoban.manage.api.enums.hm.HmPageStatus;
import com.gic.haoban.manage.service.dao.mapper.hm.TabHaobanHmPageMapper;
import com.gic.haoban.manage.service.entity.hm.TabHaobanHmPage;
import com.gic.haoban.manage.service.pojo.bo.hm.HmPageBO;
import com.gic.haoban.manage.service.pojo.qo.HmPageTemplateQO;
import com.gic.haoban.manage.service.service.hm.HmPageService;
import com.gic.redis.data.util.RedisUtil;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @Author MUSI
 * @Date 2022/7/4 1:05 PM
 * @Description
 * @Version
 **/
@Component
public class HmPageServiceImpl implements HmPageService {

    private static final Logger log = LoggerFactory.getLogger(HmPageServiceImpl.class);

    @Autowired
    private TabHaobanHmPageMapper haobanHmPageMapper;

    private static final String TEMPLATE_CODE_PREFIX = "MB01";

    private static final String PAGE_CACHE_KEY_PRE = "HM:page:";

    private static final Long PAGE_CACHE_KEY_EXPIRE_TIME = 5 * 60L;

    /**
     * 根据pageId 查询落地页详情
     * @param pageId
     * @return
     */
    public HmPageBO queryHmPageDetail(Long pageId){
        String pageCacheKey = PAGE_CACHE_KEY_PRE + pageId;
        HmPageBO pageCache = RedisUtil.getCache(pageCacheKey, HmPageBO.class);
        if (pageCache != null){
            return pageCache;
        }
        TabHaobanHmPage haobanHmPage = haobanHmPageMapper.selectByPrimaryKey(pageId);
        if (haobanHmPage == null){
            return null;
        }
        HmPageBO pageBo = EntityUtil.changeEntityByOrika(HmPageBO.class, haobanHmPage);
        RedisUtil.setCache(pageCacheKey, pageBo, PAGE_CACHE_KEY_EXPIRE_TIME, TimeUnit.SECONDS);
        return pageBo;
    }

    /**
     * 条件查询落地页模板
     *
     * @param pageTemplateQo
     * @return
     */
    @Override
    public Page<HmPageBO> queryPageList(HmPageTemplateQO pageTemplateQo) {
        PageHelper.startPage(pageTemplateQo.getPageNum(), pageTemplateQo.getPageSize());
        com.github.pagehelper.Page<TabHaobanHmPage> tabHaobanHmPages =
                (com.github.pagehelper.Page<TabHaobanHmPage>) haobanHmPageMapper.queryHmPageTemplateList(pageTemplateQo);
        return PageUtil.changePageHelperToCurrentPage(tabHaobanHmPages, HmPageBO.class);
    }

    /**
     * 删除落地页模板
     *
     * @param pageDTO
     */
    @Override
    public void deleteHmPage(HmPageDTO pageDTO) {
        TabHaobanHmPage tempHmPage = new TabHaobanHmPage();
        tempHmPage.setPageId(pageDTO.getPageId());
        tempHmPage.setStatus(HmPageStatus.DISABLE.getCode());
        tempHmPage.setUpdateTime(new Date());
        tempHmPage.setModifierId(pageDTO.getModifierId());
        tempHmPage.setModifierName(pageDTO.getModifierName());
        log.info("[deleteHmPageTemplate] params:{}", JSON.toJSONString(tempHmPage));
        haobanHmPageMapper.updateByPrimaryKeySelective(tempHmPage);
        this.removeCache(pageDTO.getPageId());
    }

    private void removeCache(Long pageId){
        String pageCacheKey = PAGE_CACHE_KEY_PRE + pageId;
        RedisUtil.delCache(pageCacheKey);
    }

    /**
     * 保存
     *
     * @param pageDTO
     * @return
     */
    @Override
    public Long saveOrUpdateHmPage(HmPageDTO pageDTO) {
        log.info("[saveOrUpdateHmPageTemplate] params:{}", JSON.toJSONString(pageDTO));
        Date now = new Date();
        if (Objects.isNull(pageDTO.getPageId())) {
            // save
            TabHaobanHmPage haobanHmPage = new TabHaobanHmPage();
            BeanUtils.copyProperties(pageDTO, haobanHmPage);
            haobanHmPage.setPageId(UniqueIdUtils.uniqueLong());
            haobanHmPage.setPageCode(this.generatorTemplateCode(haobanHmPage.getPageId()));
            haobanHmPage.setCreateorId(pageDTO.getCreateorId());
            haobanHmPage.setCreateorName(pageDTO.getCreateorName());
            haobanHmPage.setCreateTime(now);
            haobanHmPage.setModifierId(pageDTO.getCreateorId());
            haobanHmPage.setModifierName(pageDTO.getCreateorName());
            haobanHmPage.setUpdateTime(now);
            haobanHmPage.setStatus(HmPageStatus.ENABLE.getCode());
            haobanHmPageMapper.insert(haobanHmPage);
            return haobanHmPage.getPageId();
        } else {
            // update
            TabHaobanHmPage haobanHmPage = new TabHaobanHmPage();
            BeanUtils.copyProperties(pageDTO, haobanHmPage);
            haobanHmPage.setPageId(pageDTO.getPageId());
            haobanHmPage.setPageCode(null);
            haobanHmPage.setUpdateTime(now);
            haobanHmPage.setModifierId(pageDTO.getModifierId());
            haobanHmPage.setModifierName(pageDTO.getModifierName());
            haobanHmPageMapper.updateByPrimaryKeySelective(haobanHmPage);
            this.removeCache(pageDTO.getPageId());
            return haobanHmPage.getPageId();
        }
    }

    /**
     * 生成模板code
     * @return
     */
    private String generatorTemplateCode(Long templateId){
        return TEMPLATE_CODE_PREFIX + templateId;
    }
}
