package io.hmit.modules.area.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.hmit.common.constant.Constant;
import io.hmit.common.exception.ErrorCode;
import io.hmit.common.exception.HmitException;
import io.hmit.common.service.impl.CrudServiceImpl;
import io.hmit.common.utils.ConvertUtils;
import io.hmit.common.utils.HttpContextUtils;
import io.hmit.common.utils.TreeUtils;
import io.hmit.modules.area.dao.AreaDao;
import io.hmit.modules.area.dto.AreaDTO;
import io.hmit.modules.area.entity.AreaEntity;
import io.hmit.modules.area.service.AreaService;
import io.hmit.modules.sys.service.SysLanguageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 区市管理
 *
 * @author syf syf0412@qq.com
 * @since 1.0.0 2020-12-25
 */
@Service
public class AreaServiceImpl extends CrudServiceImpl<AreaDao, AreaEntity, AreaDTO> implements AreaService {

    @Autowired
    private SysLanguageService sysLanguageService;

    @Override
    public QueryWrapper<AreaEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");

        QueryWrapper<AreaEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);

        return wrapper;
    }


    @Override
    public List<AreaDTO> getAllAreaList(Integer type) {
        List<AreaEntity> areaList = baseDao.getAreaList(type);

        List<AreaDTO> dtoList = ConvertUtils.sourceToTarget(areaList, AreaDTO.class);

        return TreeUtils.build(dtoList, Constant.AREA_ROOT);
    }

    @Override
    public List<AreaDTO> getListPid(Long pid) {
        List<AreaEntity> areaList = baseDao.getListPid(pid);

        return ConvertUtils.sourceToTarget(areaList, AreaDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(AreaDTO dto) {

        AreaEntity entity = ConvertUtils.sourceToTarget(dto, AreaEntity.class);

        //保存菜单
        insert(entity);
        saveLanguage(entity.getId(), "name", entity.getName());
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AreaDTO dto) {

        AreaEntity entity = ConvertUtils.sourceToTarget(dto, AreaEntity.class);

        //上级菜单不能为自身
        if (entity.getId().equals(entity.getPid())) {
            throw new HmitException(ErrorCode.SUPERIOR_MENU_ERROR);
        }

        //更新菜单
        updateById(entity);
        saveLanguage(entity.getId(), "name", entity.getName());
    }

    private void saveLanguage(Long tableId, String fieldName, String fieldValue) {

        sysLanguageService.saveOrUpdate("wl_area", tableId, fieldName, fieldValue, HttpContextUtils.getLanguage());
    }
}
