package com.iotechn.unimall.admin.api.sys;


import cn.hutool.json.JSONUtil;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.model.Page;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.data.domain.SysDictDataDO;
import com.iotechn.unimall.data.mapper.SysDictDataMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.apache.ibatis.session.RowBounds;

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

/**
 * Generate Code By Unimall
 */
@Service
public class DictServiceImpl implements DictService {

    @Autowired
    private SysDictDataMapper sysDictDataMapper;
    @Override
    public boolean delete(Long id,  Long adminId) throws ServiceException {
        return sysDictDataMapper.deleteById(id) > 0;
    }

    @Override
    public List<SysDictDataDO> getDictDataList(String dictType,Long adminId,Long companyId) {
        List<SysDictDataDO> dictDataList = new ArrayList<>();
        Wrapper<SysDictDataDO> wrapper = new EntityWrapper<SysDictDataDO>();
        if (dictType != null) {
            wrapper.eq("dict_type", dictType);
        }
        wrapper.eq("company_id", companyId);
//        wrapper.orderBy("sort",true);
        wrapper.orderBy("dict_type",true);
        wrapper.orderBy("value",true);
        dictDataList = sysDictDataMapper.selectList(wrapper);
        return dictDataList;
    }
    @Override
    public Page<SysDictDataDO> list(String dictName, String label, Integer page, Integer limit, Long adminId,Long companyId) throws ServiceException {
        Wrapper<SysDictDataDO> wrapper = new EntityWrapper<SysDictDataDO>();
        if (dictName != null) {
            wrapper.like("dict_name", dictName);
        }
        if (label != null) {
            wrapper.like("label", label);
        }
        wrapper.orderBy("dict_type",true);
        wrapper.orderBy("value",true);
        wrapper.eq("company_id",companyId);
        List<SysDictDataDO> list = sysDictDataMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        Integer count = sysDictDataMapper.selectCount(wrapper);
        return new Page<SysDictDataDO>(list, page, limit, count);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysDictDataDO create(String dictName,String dictType, String label, String value, Integer sort, Long adminId,Long companyId) throws ServiceException {
         Date now = new Date();
         SysDictDataDO insertDO = new SysDictDataDO();
         insertDO.setDictName(dictName);
         insertDO.setDictType(dictType);
         insertDO.setLabel(label);
         insertDO.setValue(value);
         insertDO.setSort(sort);
         insertDO.setCompanyId(companyId);
         insertDO.setGmtUpdate(now);
         insertDO.setGmtCreate(now);
         if (sysDictDataMapper.insert(insertDO) > 0) {
             return insertDO;
         }
         throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String edit(Long id, String dictName,String dictType, String label, String value, Integer sort, Long adminId,Long companyId) throws ServiceException {
         Date now = new Date();
         SysDictDataDO updateDO = new SysDictDataDO();
         updateDO.setId(id);
         updateDO.setDictName(dictName);
         updateDO.setDictType(dictType);
         updateDO.setLabel(label);
         updateDO.setValue(value);
         updateDO.setCompanyId(companyId);
         updateDO.setSort(sort);
         updateDO.setGmtUpdate(now);
         if (sysDictDataMapper.updateById(updateDO) > 0) {
             return "ok";
         }
         throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

}
