package com.cm.common.service.templaterecord.impl;

import com.cm.common.base.AbstractService;
import com.cm.common.dao.templaterecord.ITemplateRecordDao;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SearchException;
import com.cm.common.pojo.ListPage;
import com.cm.common.pojo.dtos.templaterecord.TemplateRecordDTO;
import com.cm.common.pojo.vos.templaterecord.TemplateRecordVO;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultList;
import com.cm.common.service.templaterecord.ITemplateRecordService;
import com.cm.common.util.FreemarkerTemplateProperties;
import com.cm.common.utils.HashMapUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: TemplateRecordServiceImpl
 * @Description: 模板构建记录
 * @Author: WenG
 * @Date: 2020-05-07 16:50
 * @Version: 1.0
 **/
@Service
public class TemplateRecordServiceImpl extends AbstractService implements ITemplateRecordService {

    @Autowired
    private ITemplateRecordDao templateRecordDao;
    @Autowired
    private FreemarkerTemplateProperties freemarkerTemplateProperties;

    @Override
    public SuccessResult saveTemplateRecord(TemplateRecordVO templateRecordVO) throws Exception {
        saveTemplateRecordInfo(null, templateRecordVO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult saveTemplateRecordByToken(String token, TemplateRecordVO templateRecordVO) throws Exception {
        saveTemplateRecordInfo(token, templateRecordVO);
        return new SuccessResult();
    }

    /**
     * 新增模板构建记录
     *
     * @param token
     * @param templateRecordVO
     * @throws Exception
     */
    private void saveTemplateRecordInfo(String token, TemplateRecordVO templateRecordVO) throws Exception {
        Map<String, Object> params = HashMapUtil.beanToMap(templateRecordVO);
        TemplateRecordDTO recordDto = templateRecordDao.getTemplateRecord(params);
        // 不存在该业务的模板文件
        if(recordDto == null) {
                    if (token != null) {
                        setSaveInfo(token, params);
                    } else {
                        setSaveInfo(params);
            }
            templateRecordDao.saveTemplateRecord(params);
        }else{
            if (token != null) {
                setUpdateInfo(token, params);
            } else {
                setUpdateInfo(params);
            }
            templateRecordDao.updateTemplateRecord(params);
        }
    }

    @Override
    public SuccessResult removeTemplateRecord(String ids) throws RemoveException {
        removeTemplateRecordInfo(null, ids);
        return new SuccessResult();
    }

    @Override
    public SuccessResult removeTemplateRecordByToken(String token, String ids) throws RemoveException {
        removeTemplateRecordInfo(token, ids);
        return new SuccessResult();
    }

    /**
     * 删除模板构建记录
     *
     * @param token
     * @param ids
     */
    private void removeTemplateRecordInfo(String token, String ids) {
        Map<String, Object> params = getHashMap(3);
        params.put("templateRecordIds", Arrays.asList(ids.split("_")));
        if (token != null) {
            setUpdateInfo(token, params);
        } else {
            setUpdateInfo(params);
        }
        templateRecordDao.removeTemplateRecord(params);
    }

    @Override
    public SuccessResult updateTemplateRecord(String templateRecordId, TemplateRecordVO templateRecordVO) throws Exception {
        updateTemplateRecordInfo(null, templateRecordId, templateRecordVO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult updateTemplateRecordByToken(String token, String templateRecordId, TemplateRecordVO templateRecordVO) throws Exception {
        updateTemplateRecordInfo(token, templateRecordId, templateRecordVO);
        return new SuccessResult();
    }

    /**
     * 修改模板构建记录
     *
     * @param token
     * @param templateRecordId
     * @param templateRecordVO
     */
    private void updateTemplateRecordInfo(String token, String templateRecordId, TemplateRecordVO templateRecordVO) throws Exception {
        Map<String, Object> params = HashMapUtil.beanToMap(templateRecordVO);
        params.put("templateRecordId", templateRecordId);
        if (token != null) {
            setUpdateInfo(token, params);
        } else {
            setUpdateInfo(params);
        }
        templateRecordDao.updateTemplateRecord(params);
    }

    @Override
    public TemplateRecordDTO getTemplateRecordById(String templateRecordId) throws SearchException {
        Map<String, Object> params = super.getHashMap(1);
        params.put("templateRecordId", templateRecordId);
        TemplateRecordDTO dto = templateRecordDao.getTemplateRecord(params);
        dto.setTemplateRecordUrlNginx(freemarkerTemplateProperties.getNginxAddress() + dto.getTemplateRecordUrl());
        return dto;
    }

    @Override
    public List<TemplateRecordDTO> listTemplateRecord(Map<String, Object> params) throws SearchException {
        return templateRecordDao.listTemplateRecord(params);
    }

    @Override
    public SuccessResultList<List<TemplateRecordDTO>> listPageTemplateRecord(ListPage page) throws SearchException {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<TemplateRecordDTO> templateRecordDTOs = templateRecordDao.listTemplateRecord(page.getParams());
        for(TemplateRecordDTO dto : templateRecordDTOs) {
            dto.setTemplateRecordUrlNginx(freemarkerTemplateProperties.getNginxAddress() + dto.getTemplateRecordUrl());
        }
        PageInfo<TemplateRecordDTO> pageInfo = new PageInfo<>(templateRecordDTOs);
        return new SuccessResultList<>(templateRecordDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public List<TemplateRecordDTO> getTemplateRecordByTemplateId(String templateId) throws SearchException {
        Map<String, Object> params = super.getHashMap(1);
        params.put("templateId", templateId);
        return templateRecordDao.getTemplateRecordByTemplateId(params);
    }

}
