package com.xhkjedu.sapi.service.system;

import com.xhkjedu.sapi.mapper.system.AreazoneMapper;
import com.xhkjedu.sapi.model.system.TAreazone;
import com.xhkjedu.utils.N_Utils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author ywx
 * @classname AreazoneService
 * @description 
 * @date 2021/4/8 9:28
 **/
@Service
public class AreazoneService {
    @Resource
    private AreazoneMapper areazoneMapper;

    //添加
    public void save(TAreazone model) {
        model.setCreatetime(N_Utils.getSecondTimestamp());
        areazoneMapper.insertSelective(model);
    }

    //删除
    public Integer deleteById(Integer id) {
        return areazoneMapper.deleteByPrimaryKey(id);
    }

    //修改
    public Integer update(TAreazone model) {
        TAreazone areazone = findById(model.getAreaid());
        if (!areazone.getAreaname().equals(model.getAreaname())) {//省份、市地区名称修改
            Integer level = model.getLevel();
            Integer areaid = model.getAreaid();
            String areaall = model.getAreaall();
            if (level == 1) {//省份地区名称修改
                areazoneMapper.updateAreaAll(areaid, areaall);//修改市地区全名称
                areazoneMapper.updateAreaAll2(areaid, areaall);//修改区、县地区全名称
            } else if (level == 2) {//市地区名称修改
                areazoneMapper.updateAreaAll(areaid, areaall);//修改区、县地区全名称
            }
        }
        return areazoneMapper.updateByPrimaryKeySelective(model);
    }

    //获取详情
    public TAreazone findById(Integer id) {
        return areazoneMapper.selectByPrimaryKey(id);
    }

    //获取地区列表
    public List <Map> listAll() {
        return areazoneMapper.listAll();
    }

    //获取地区选择列表
    public List<Map> listForSel() {
        return areazoneMapper.listForSel();
    }
}
