package com.energy.manage.service.controller.area;

import com.alibaba.fastjson.JSONObject;
import com.energy.manage.common.po.area.AreaPO;
import com.energy.manage.common.reponse.RespMsg;
import com.energy.manage.service.config.annotations.UserLoginToken;
import com.energy.manage.service.controller.base.BaseServiceController;
import com.energy.manage.service.service.area.AreaApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author generate-code
 * @date 2020-07-29 11:28:12
 */
@Slf4j
@RestController
@CrossOrigin
@Api(value = "CustomerController", tags = "区域接口")
@RequestMapping(value = "/api/area")
public class AreaApiController extends BaseServiceController {

    @Value("${spring.datasource.druid.url}")
    private String druid;

    @Autowired
    private AreaApiService areaService;

    @UserLoginToken
    @PostMapping(value = "findAllProvince")
    @ApiOperation(value = "获取所有的省份信息")
    public JSONObject findAllProvince() {
        List<AreaPO> list = areaService.findAllProvince();
        list = CollectionUtils.isEmpty(list) ? Collections.EMPTY_LIST : list;
        return RespMsg.SUCCESS(list);
    }

    @UserLoginToken
    @GetMapping(value = "getCitiesByPId")
    @ApiOperation(value = "根据省份Id获取对应的城市信息")
    public JSONObject getCitiesByPId(@RequestParam Integer provinceId) {
        List<AreaPO> list = areaService.selectCityByPId(provinceId);
        list = CollectionUtils.isEmpty(list) ? Collections.EMPTY_LIST : list;
        return RespMsg.SUCCESS(list);
    }

    @UserLoginToken
    @GetMapping(value = "getAreasByCId")
    @ApiOperation(value = "根据城市Id获取对应的区县信息")
    public JSONObject getAreasByCId(@RequestParam Integer cityId) {
        List<AreaPO> list = areaService.selectAreaByCId(cityId);
        list = CollectionUtils.isEmpty(list) ? Collections.EMPTY_LIST : list;
        return RespMsg.SUCCESS(list);
    }

    @UserLoginToken
    @GetMapping(value = "updateAreaByAId")
    @ApiOperation(value = "根据区县Id修改区县信息", notes = "areaType 2市   3 县")
    public JSONObject updateAreaByAId(@RequestParam Integer areaId,
                                      @RequestParam String area,
                                      @RequestParam Integer areaType) {
        if (StringUtils.isEmpty(area) || !Optional.ofNullable(areaId).isPresent()) {
            return RespMsg.NOTNULLPARAM();
        }
        if (2 != areaType && 3 != areaType) {
            return RespMsg.INVALIDPARAM("type");
        }
        boolean flag = areaService.updateAreaByAId(areaId, area, areaType);
        if (flag) {
            return RespMsg.SUCCESS();
        }
        return RespMsg.FAIL();
    }

    @UserLoginToken
    @PostMapping(value = "addArea")
    @ApiOperation(value = "添加省市区县", notes = "areaType 2市   3 县")
    public JSONObject addArea(@RequestParam Integer provinceId,
                              @RequestParam String province,
                              @RequestParam(required = false) Integer cityId,
                              @RequestParam(required = false) String city,
                              @RequestParam(required = false) String area,
                              @RequestParam Integer areaType) {
        int type = areaType.intValue();
        if (2 != type && 3 != type) {
            return RespMsg.INVALIDPARAM("type");
        }
        if (2 == type && StringUtils.isEmpty(city)) {
            return RespMsg.NOTNULLPARAM();
        }
        if (3 == type && (!Optional.ofNullable(cityId).isPresent() || StringUtils.isEmpty(city) || StringUtils.isEmpty(area))) {
            return RespMsg.NOTNULLPARAM();
        }
        boolean flag = areaService.addArea(provinceId, province, cityId, city, area, type);
        if (flag) {
            return RespMsg.SUCCESS();
        }
        return RespMsg.FAIL();
    }

    @UserLoginToken
    @PostMapping(value = "deleteAreaByAId")
    @ApiOperation(value = "根据区县Id删除区县信息")
    public JSONObject deleteAreaByAId(@RequestParam Integer areaId,
                                      @RequestParam Integer areaType) {
        if (!Optional.ofNullable(areaId).isPresent()) {
            return RespMsg.NOTNULLPARAM();
        }
        return areaService.deleteAreaByAId(areaId, areaType);
    }

    @UserLoginToken
    @PostMapping(value = "getEnableProvince")
    @ApiOperation(value = "获取未被占用的省份")
    public JSONObject getEnableProvince(@RequestParam(required = false) String provinceName) {
        List<AreaPO> list = areaService.getEnableProvince(provinceName);
        return RespMsg.SUCCESS(list);
    }

    @UserLoginToken
    @PostMapping(value = "getEnableArea")
    @ApiOperation(value = "获取全部省份")
    public JSONObject getEnableArea() {
        return areaService.getEnableArea();
    }

    @UserLoginToken
    @PostMapping(value = "syncAreaCode")
    @ApiOperation(value = "同步区号")
    public JSONObject syncAreaCode(@RequestParam String state) {

        System.out.println(this.druid);
        boolean flg = false;
        if(state.equals("true")){
            log.info(" ===== 开始同步区号  ===== ");
            flg = areaService.syncAreaCode();
        }
        return RespMsg.SUCCESS(flg);
    }

}
