package cn.com.tenlion.freemarker.service.templaterecord.impl;

import cn.com.tenlion.freemarker.dao.templaterecord.ITemplateRecordDao;
import cn.com.tenlion.freemarker.pojo.dtos.templaterecord.TemplateRecordDTO;
import cn.com.tenlion.freemarker.pojo.vos.templaterecord.TemplateRecordVO;
import cn.com.tenlion.freemarker.service.templaterecord.ITemplateRecordService;
import cn.com.tenlion.freemarker.util.FreemarkerTemplateProperties;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.RemoveException;
import ink.wgink.exceptions.SearchException;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.map.HashMapUtil;
import org.apache.commons.lang3.StringUtils;
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 DefaultBaseService implements ITemplateRecordService {

    @Autowired
    private ITemplateRecordDao templateRecordDao;

    @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 (StringUtils.isBlank(token)) {
                setSaveInfo(params);
            } else {
                setAppSaveInfo(token, params);
            }
            templateRecordDao.saveTemplateRecord(params);
        }else{
            if (StringUtils.isBlank(token)) {
                setUpdateInfo(params);
            } else {
                setAppUpdateInfo(token, 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 (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, 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 (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, 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);
        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());
        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);
    }

}
