package com.cm.common.plugin.service.dynamic.config.form.impl;

import com.cm.common.base.AbstractService;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SearchException;
import com.cm.common.exception.UpdateException;
import com.cm.common.plugin.dao.dynamic.config.form.IDynamicConfigFormDao;
import com.cm.common.plugin.pojo.dtos.dynamic.config.form.DynamicConfigFormDTO;
import com.cm.common.plugin.pojo.vos.dynamic.config.form.DynamicConfigFormVO;
import com.cm.common.plugin.pojo.vos.dynamic.config.form.FieldShowVO;
import com.cm.common.plugin.service.dynamic.config.form.IDynamicConfigFormService;
import com.cm.common.pojo.ListPage;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultList;
import com.cm.common.utils.HashMapUtil;
import com.cm.common.utils.UUIDUtil;
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;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: DynamicConfigFormServiceImpl
 * @Description: 动态配置-表单
 * @Author: WangGeng
 * @Date: 2019/12/7 6:12 下午
 * @Version: 1.0
 **/
@Service
public class DynamicConfigFormServiceImpl extends AbstractService implements IDynamicConfigFormService {

    @Autowired
    private IDynamicConfigFormDao dynamicConfigFormDao;

    @Override
    public SuccessResult saveForm(DynamicConfigFormVO dynamicConfigFormVO) throws Exception {
        Map<String, Object> params = HashMapUtil.beanToMap(dynamicConfigFormVO);
        params.put("id", UUIDUtil.getUUID());
        setSaveInfo(params);
        dynamicConfigFormDao.saveForm(params);
        return new SuccessResult();
    }

    @Override
    public SuccessResult removeForm(String ids) throws RemoveException {
        Map<String, Object> params = getHashMap(3);
        params.put("ids", Arrays.asList(ids.split("_")));
        setUpdateInfo(params);
        dynamicConfigFormDao.removeForm(params);
        return new SuccessResult();
    }

    @Override
    public SuccessResult updateForm(String id, DynamicConfigFormVO dynamicConfigFormVO) throws Exception {
        Map<String, Object> params = HashMapUtil.beanToMap(dynamicConfigFormVO);
        params.put("id", id);
        setUpdateInfo(params);
        dynamicConfigFormDao.updateForm(params);
        return new SuccessResult();
    }

    @Override
    public void updateFormTableName(String newTableName, String oldTableName) throws UpdateException {
        Map<String, Object> params = getHashMap(4);
        params.put("newTableName", newTableName);
        params.put("oldTableName", oldTableName);
        setUpdateInfo(params);
        dynamicConfigFormDao.updateFormTableName(params);
    }

    @Override
    public SuccessResult updateFieldListShow(String id, FieldShowVO fieldShowVO) throws UpdateException {
        Map<String, Object> params = getHashMap(2);
        params.put("id", id);
        params.put("listShow", fieldShowVO.getShow() ? 1 : 0);
        setUpdateInfo(params);
        dynamicConfigFormDao.updateForm(params);
        return new SuccessResult();
    }

    @Override
    public SuccessResult updateFieldFormShow(String id, FieldShowVO fieldShowVO) throws UpdateException {
        Map<String, Object> params = getHashMap(2);
        params.put("id", id);
        params.put("formShow", fieldShowVO.getShow() ? 1 : 0);
        setUpdateInfo(params);
        dynamicConfigFormDao.updateForm(params);
        return new SuccessResult();
    }

    @Override
    public DynamicConfigFormDTO getFormById(String id) throws SearchException {
        Map<String, Object> params = super.getHashMap(1);
        params.put("id", id);
        return dynamicConfigFormDao.getFormById(params);
    }

    @Override
    public List<DynamicConfigFormDTO> listForm(Map<String, Object> params) throws SearchException {
        return dynamicConfigFormDao.listForm(params);
    }

    @Override
    public SuccessResultList<List<DynamicConfigFormDTO>> listPageForm(ListPage page) throws SearchException {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<DynamicConfigFormDTO> dynamicConfigFormDTOList = dynamicConfigFormDao.listForm(page.getParams());
        PageInfo<DynamicConfigFormDTO> pageInfo = new PageInfo<>(dynamicConfigFormDTOList);
        return new SuccessResultList<>(dynamicConfigFormDTOList, pageInfo.getPageNum(), pageInfo.getTotal());
    }
}
