package cn.com.tenlion.bigdata.service.bigdatatemplatearea.impl;

import cn.com.tenlion.bigdata.dao.bigdatatemplatearea.IBigdataTemplateAreaDao;
import cn.com.tenlion.bigdata.pojo.dtos.bigdatatemplatearea.BigdataTemplateAreaDTO;
import cn.com.tenlion.bigdata.pojo.vos.bigdatatemplatearea.BigdataTemplateAreaPiointVO;
import cn.com.tenlion.bigdata.pojo.vos.bigdatatemplatearea.BigdataTemplateAreaVO;
import cn.com.tenlion.bigdata.service.bigdatatemplatearea.IBigdataTemplateAreaService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.util.concurrent.AbstractService;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.exceptions.RemoveException;
import ink.wgink.exceptions.SaveException;
import ink.wgink.exceptions.SearchException;
import ink.wgink.exceptions.UpdateException;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.pojo.result.SuccessResultData;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.util.UUIDUtil;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: BigdataTemplateAreaServiceImpl
 * @Description: 大数据展示模板的区域设置
 * @Author: WenG
 * @Date: 2020-10-20 15:08
 * @Version: 1.0
 **/
@Service
public class BigdataTemplateAreaServiceImpl extends DefaultBaseService implements IBigdataTemplateAreaService {

    @Autowired
    private IBigdataTemplateAreaDao bigdataTemplateAreaDao;

    @Override
    public SuccessResult saveBigdataTemplateArea(BigdataTemplateAreaVO bigdataTemplateAreaVO) throws Exception {
        saveBigdataTemplateAreaInfo(null, bigdataTemplateAreaVO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult saveBigdataTemplateAreaByToken(String token, BigdataTemplateAreaVO bigdataTemplateAreaVO) throws Exception {
        saveBigdataTemplateAreaInfo(token, bigdataTemplateAreaVO);
        return new SuccessResult();
    }

    @Override
    public String saveBigdataTemplateAreaReturnId(BigdataTemplateAreaVO bigdataTemplateAreaVO) throws Exception {
        return saveBigdataTemplateAreaInfoReturnId(null, bigdataTemplateAreaVO);
    }

    @Override
    public String saveBigdataTemplateAreaByTokenReturnId(String token, BigdataTemplateAreaVO bigdataTemplateAreaVO) throws Exception {
        return saveBigdataTemplateAreaInfoReturnId(token, bigdataTemplateAreaVO);
    }

    /**
     * 新增大数据展示模板的区域设置
     *
     * @param token
     * @param bigdataTemplateAreaVO
     * @throws Exception
     */
    private void saveBigdataTemplateAreaInfo(String token, BigdataTemplateAreaVO bigdataTemplateAreaVO) throws Exception {
        saveBigdataTemplateAreaInfoReturnId(token, bigdataTemplateAreaVO);
    }

    /**
     * 新增大数据展示模板的区域设置
     *
     * @param token
     * @param bigdataTemplateAreaVO
     * @return bigdataTemplateAreaId
     * @throws Exception
     */
    private String saveBigdataTemplateAreaInfoReturnId(String token, BigdataTemplateAreaVO bigdataTemplateAreaVO) throws Exception {
        String bigdataTemplateAreaId = UUIDUtil.getUUID();

        bigdataTemplateAreaVO.setTemplateAreaLeft(10.0);
        bigdataTemplateAreaVO.setTemplateAreaDown(0.0);
        bigdataTemplateAreaVO.setTemplateAreaRight(0.0);
        bigdataTemplateAreaVO.setTemplateAreaTop(10.0);
        bigdataTemplateAreaVO.setTemplateAreaHeight(300.0);
        bigdataTemplateAreaVO.setTemplateAreaWidth(300.0);
        bigdataTemplateAreaVO.setTemplateAreaServerData(bigdataTemplateAreaVO.getTemplateAreaServerData().trim());

        Map<String, Object> params = HashMapUtil.beanToMap(bigdataTemplateAreaVO);
        params.put("bigdataTemplateAreaId", bigdataTemplateAreaId);
        if (StringUtils.isBlank(token)) {
            setSaveInfo(params);
        } else {
            setAppSaveInfo(token, params);
        }
        // 如果跳转详情开启了 , 那么跳转方式和跳转模板不能为空
        if(!"0".equals(bigdataTemplateAreaVO.getTemplateAreaJump())) {
            if (StringUtils.isEmpty(bigdataTemplateAreaVO.getTemplateAreaJumpMode())) {
                throw new SaveException("请选择跳转方式");
            }
            if (Integer.valueOf(bigdataTemplateAreaVO.getTemplateAreaJumpMode()) > 3) {
                if(StringUtils.isEmpty(bigdataTemplateAreaVO.getTemplateAreaJumpUrl())) {
                    throw new SaveException("缺少跳转链接");
                }
            }else{
                if(StringUtils.isEmpty(bigdataTemplateAreaVO.getTemplateAreaJumpTemplateId())) {
                    throw new SaveException("缺少跳转模板");
                }
            }
        }
        bigdataTemplateAreaDao.saveBigdataTemplateArea(params);
        return bigdataTemplateAreaId;
    }

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

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

    /**
     * 删除大数据展示模板的区域设置
     *
     * @param token
     * @param ids
     */
    private void removeBigdataTemplateAreaInfo(String token, String ids) {
        Map<String, Object> params = getHashMap(3);
        params.put("bigdataTemplateAreaIds", Arrays.asList(ids.split("_")));
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        bigdataTemplateAreaDao.removeBigdataTemplateArea(params);
    }

    @Override
    public void deleteBigdataTemplateArea(String ids) throws RemoveException {
        Map<String, Object> params = getHashMap(3);
        params.put("bigdataTemplateAreaIds", Arrays.asList(ids.split("_")));
        bigdataTemplateAreaDao.deleteBigdataTemplateArea(params);
    }

    @Override
    public SuccessResult updateBigdataTemplateArea(String bigdataTemplateAreaId, BigdataTemplateAreaVO bigdataTemplateAreaVO) throws Exception {
        updateBigdataTemplateAreaInfo(null, bigdataTemplateAreaId, bigdataTemplateAreaVO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult updateBigdataTemplateAreaByToken(String token, String bigdataTemplateAreaId, BigdataTemplateAreaVO bigdataTemplateAreaVO) throws Exception {
        updateBigdataTemplateAreaInfo(token, bigdataTemplateAreaId, bigdataTemplateAreaVO);
        return new SuccessResult();
    }

    /**
     * 修改大数据展示模板的区域设置
     *
     * @param token
     * @param bigdataTemplateAreaId
     * @param bigdataTemplateAreaVO
     */
    private void updateBigdataTemplateAreaInfo(String token, String bigdataTemplateAreaId, BigdataTemplateAreaVO bigdataTemplateAreaVO) throws Exception {
        Map<String, Object> params = HashMapUtil.beanToMap(bigdataTemplateAreaVO);
        bigdataTemplateAreaVO.setTemplateAreaServerData(bigdataTemplateAreaVO.getTemplateAreaServerData().trim());
        params.put("bigdataTemplateAreaId", bigdataTemplateAreaId);
        if (StringUtils.isBlank(token)) {
            setUpdateInfo(params);
        } else {
            setAppUpdateInfo(token, params);
        }
        // 如果跳转详情开启了 , 那么跳转方式和跳转模板不能为空
        if(!"0".equals(bigdataTemplateAreaVO.getTemplateAreaJump())) {
            if (StringUtils.isEmpty(bigdataTemplateAreaVO.getTemplateAreaJumpMode())) {
                throw new SaveException("请选择跳转方式");
            }
            if (Integer.valueOf(bigdataTemplateAreaVO.getTemplateAreaJumpMode()) > 3) {
                if(StringUtils.isEmpty(bigdataTemplateAreaVO.getTemplateAreaJumpUrl())) {
                    throw new SaveException("缺少跳转链接");
                }
            }else{
                if(StringUtils.isEmpty(bigdataTemplateAreaVO.getTemplateAreaJumpTemplateId())) {
                    throw new SaveException("缺少跳转模板");
                }
            }
        }
        bigdataTemplateAreaDao.updateBigdataTemplateArea(params);
    }

    @Override
    public BigdataTemplateAreaDTO getBigdataTemplateAreaById(String bigdataTemplateAreaId) throws SearchException {
        Map<String, Object> params = super.getHashMap(1);
        params.put("bigdataTemplateAreaId", bigdataTemplateAreaId);
        return bigdataTemplateAreaDao.getBigdataTemplateArea(params);
    }

    @Override
    public List<BigdataTemplateAreaDTO> listBigdataTemplateArea(Map<String, Object> params) throws SearchException {
        return bigdataTemplateAreaDao.listBigdataTemplateArea(params);
    }

    @Override
    public SuccessResultList<List<BigdataTemplateAreaDTO>> listPageBigdataTemplateArea(ListPage page) throws SearchException {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<BigdataTemplateAreaDTO> bigdataTemplateAreaDTOs = bigdataTemplateAreaDao.listBigdataTemplateArea(page.getParams());
        PageInfo<BigdataTemplateAreaDTO> pageInfo = new PageInfo<>(bigdataTemplateAreaDTOs);
        return new SuccessResultList<>(bigdataTemplateAreaDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

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

    @Override
    public SuccessResultData<Integer> countBigdataTemplateArea(Map<String, Object> params) throws SearchException {
        return new SuccessResultData<>(countNumberBigdataTemplateArea(params));
    }

    @Override
    public List<BigdataTemplateAreaDTO> getBigdataTemplateAreaByBigdataTemplateId(String bigdataTemplateId) throws SearchException {
        Map<String, Object> params = super.getHashMap(1);
        params.put("bigdataTemplateId", bigdataTemplateId);
        return bigdataTemplateAreaDao.getBigdataTemplateAreaByBigdataTemplateId(params);
    }

    @Override
    public SuccessResult updateBigdataTemplateAreaPoint(String bigdataTemplateAreaId, BigdataTemplateAreaPiointVO bigdataTemplateAreaPiointVO) throws Exception {
        /**
         * 算出下，与 右
         */
        Double down = bigdataTemplateAreaPiointVO.getTemplateAreaLeft() + bigdataTemplateAreaPiointVO.getTemplateAreaWidth();
        bigdataTemplateAreaPiointVO.setTemplateAreaDown(down);
        Double right = bigdataTemplateAreaPiointVO.getTemplateAreaTop() + bigdataTemplateAreaPiointVO.getTemplateAreaHeight();
        bigdataTemplateAreaPiointVO.setTemplateAreaRight(right);
        Map<String, Object> params = HashMapUtil.beanToMap(bigdataTemplateAreaPiointVO);
        params.put("bigdataTemplateAreaId", bigdataTemplateAreaId);
        setUpdateInfo(params);
        bigdataTemplateAreaDao.updateBigdataTemplateAreaPoint(params);
        return new SuccessResult();
    }

    @Override
    public SuccessResult copyBigdataTemplateAreaPoint(String bigdataTemplateAreaId) throws Exception {
        BigdataTemplateAreaDTO dto = getBigdataTemplateAreaById(bigdataTemplateAreaId);
        if(dto == null) {
            throw new UpdateException("需要复制的区域不存在");
        }
        String newBigdataTemplateAreaId = UUIDUtil.getUUID();
        dto.setTemplateAreaLeft(10.0);
        dto.setTemplateAreaDown(0.0);
        dto.setTemplateAreaRight(0.0);
        dto.setTemplateAreaTop(10.0);
        dto.setTemplateAreaHeight(300.0);
        dto.setTemplateAreaWidth(300.0);
        dto.setBigdataTemplateAreaId(newBigdataTemplateAreaId);
        Map<String, Object> params = HashMapUtil.beanToMap(dto);
        setSaveInfo(params);
        bigdataTemplateAreaDao.saveBigdataTemplateArea(params);
        return new SuccessResult();
    }

    @Override
    public SuccessResult updateBigdataTemplateAreaPointModuleData(String bigdataTemplateAreaId, String templateAreaModuleData) throws Exception {
        BigdataTemplateAreaDTO dto = getBigdataTemplateAreaById(bigdataTemplateAreaId);
        if(dto == null) {
            throw new UpdateException("需要更改的区域不存在");
        }
        Map<String, Object> params = new HashMap();
        params.put("bigdataTemplateAreaId", bigdataTemplateAreaId);
        params.put("templateAreaModuleData", templateAreaModuleData);
        setUpdateInfo(params);
        bigdataTemplateAreaDao.updateBigdataTemplateAreaPointModuleData(params);
        return new SuccessResult();
    }

}