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

import cn.hutool.core.collection.CollectionUtil;
import com.gic.commons.util.EntityUtil;
import com.gic.haoban.common.utils.UuidUtil;
import com.gic.haoban.manage.api.dto.MaidianDictDTO;
import com.gic.haoban.manage.api.dto.MaidianDictModuleDTO;
import com.gic.haoban.manage.api.dto.MaidianDictPageDTO;
import com.gic.haoban.manage.api.service.MaidianDictApiService;
import com.gic.haoban.manage.service.dao.mapper.TabHaobanMaidianDictMapper;
import com.gic.haoban.manage.service.dao.mapper.TabHaobanMaidianDictModuleMapper;
import com.gic.haoban.manage.service.entity.TabHaobanMaidianDict;
import com.gic.haoban.manage.service.entity.TabHaobanMaidianDictModule;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by tgs on 2020/2/9.
 */
@Service
public class MaidianDictApiServiceImpl implements MaidianDictApiService {
    @Autowired
    private TabHaobanMaidianDictMapper tabHaobanMaidianDictMapper;
    @Autowired
    private TabHaobanMaidianDictModuleMapper tabHaobanMaidianDictModuleMapper;

    @Override
    public void saveModule(MaidianDictModuleDTO dto) {
        TabHaobanMaidianDictModule tab = EntityUtil.changeEntityByJSON(TabHaobanMaidianDictModule.class, dto);
        if (StringUtils.isEmpty(tab.getModuleId())) {
            //新增
            tab.setCreateTime(new Date());
            tab.setUpdateTime(new Date());
            tab.setStatus(1);
            tab.setModuleId(UuidUtil.randomUUID());
            tabHaobanMaidianDictModuleMapper.insert(tab);
        } else {
            //更新
            tab.setUpdateTime(new Date());
            tabHaobanMaidianDictModuleMapper.updateByPrimaryKeySelective(tab);
        }
    }

    @Override
    public void savePage(MaidianDictDTO dto) {
        TabHaobanMaidianDict tab = EntityUtil.changeEntityByJSON(TabHaobanMaidianDict.class, dto);
        if (StringUtils.isEmpty(tab.getDictId())) {
            //新增
            tab.setCreateTime(new Date());
            tab.setUpdateTime(new Date());
            tab.setStatus(1);
            tab.setDictId(UuidUtil.randomUUID());
            tabHaobanMaidianDictMapper.insert(tab);
        } else {
            //更新
            tab.setUpdateTime(new Date());
            tabHaobanMaidianDictMapper.updateByPrimaryKeySelective(tab);
        }

    }

    @Override
    public void delPage(String dictId) {
        tabHaobanMaidianDictMapper.deleteByPrimaryKey(dictId);
    }

    @Override
    public void delModule(String moduleId) {
        tabHaobanMaidianDictModuleMapper.deleteByPrimaryKey(moduleId);
    }

    @Override
    public List<MaidianDictPageDTO> dictPage() {
        List<TabHaobanMaidianDictModule> list = tabHaobanMaidianDictModuleMapper.list();
        List<MaidianDictPageDTO> result = EntityUtil.changeEntityListByJSON(MaidianDictPageDTO.class, list);
        if (CollectionUtil.isEmpty(result)) {
            return result;
        }
        for (MaidianDictPageDTO dto : result) {
            String moduleId = dto.getModuleId();
            List<TabHaobanMaidianDict> dictList = tabHaobanMaidianDictMapper.listByModuleId(moduleId);
            List<MaidianDictDTO> subList = EntityUtil.changeEntityListByJSON(MaidianDictDTO.class, dictList);
            dto.setPageList(subList);
        }
        return result;
    }

}
