package cn.pluss.platform.controller;

import cn.pluss.platform.api.PageInfo;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.constants.ProjectConstant;
import cn.pluss.platform.entity.NewArea;
import cn.pluss.platform.leshua.LeshuaArea;
import cn.pluss.platform.leshua.LeshuaCity;
import cn.pluss.platform.leshua.LeshuaProvince;
import cn.pluss.platform.newArea.NewAreaService;
import cn.pluss.platform.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

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


@RestController
@RequestMapping("/newArea")
@Api(value = "NewArea控制类", tags = {"NewArea接口"})
public class NewAreaController {

    @Resource
    private NewAreaService newAreaService;

    @GetMapping
    @ApiOperation(value = "获取全部", notes = "返回分页过后的数据", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "查询页码", paramType = "query", dataType = "Integer", defaultValue = "0"),
            @ApiImplicitParam(name = "size", value = "每页数据量", paramType = "query", dataType = "Integer", defaultValue = "0")
    })
    public Result<PageInfo<NewArea>> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        if (size == null || size == 0) {
            size = ProjectConstant.PAGE_SIZE;
        }
        if (page == null || page == 0) {
            page = 1;
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pageSize", size);
        map.put("offset", (page - 1) * size);
        List<NewArea> newAreaList = newAreaService.queryNewAreaPage(map);
        Integer count = newAreaService.queryNewAreaPageCount(map);
        //返回参数
        PageInfo pager = new PageInfo<NewArea>(count.longValue(), (long) StringUtil.getPageCount(count, size), page.longValue(), size.longValue(), newAreaList);
        return ResultGenerator.genSuccessResult(pager);
    }

    @PostMapping
    @ApiOperation(value = "添加数据", notes = "添加新的数据", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "NewArea", value = "待添加的NewArea实例", paramType = "body", dataType = "NewArea", required = true)
    })
    public Result add(@RequestBody NewArea newArea) {
        newAreaService.saveNewArea(newArea);
        return ResultGenerator.genSuccessResult();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除数据", notes = "根据id删除数据", httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "查询的id实例", paramType = "path", dataType = "Integer", required = true)
    })
    public Result delete(@PathVariable Integer id) {
        NewArea newArea = new NewArea();
        newArea.setId(id.intValue());
        newAreaService.deleteNewArea(newArea);
        return ResultGenerator.genSuccessResult();
    }

    @PutMapping
    @ApiOperation(value = "更新数据", notes = "根据内容更新数据", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "NewArea", value = "更新的NewArea实例", paramType = "body", dataType = "NewArea", required = true)
    })
    public Result update(@RequestBody NewArea newArea) {
        newAreaService.updateNewArea(newArea);
        return ResultGenerator.genSuccessResult();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "获取单个值", notes = "查看单个项目的内容", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "查询的id", paramType = "path", required = true, dataType = "Integer", defaultValue = "0")
    })
    public Result detail(@PathVariable Long id) {
        NewArea newArea = new NewArea();
        newArea.setId(id.intValue());
        newArea = newAreaService.queryNewArea(newArea);
        return ResultGenerator.genSuccessResult(newArea);
    }


    public Result queryProvice() {
        NewArea newArea = new NewArea();
        newArea.setParentId(0);
        List<NewArea> queryNewAreaList = newAreaService.queryNewAreaList(newArea);//省

        for (NewArea newArea2 : queryNewAreaList) {

        }
        return null;

    }

    @PostMapping("/queryProvinceandCityandArea")
    @ApiOperation(value = "查询省市区", notes = "查询省市区", httpMethod = "POST")
    public Result queryProvinceandCityandArea() {
        NewArea newArea = new NewArea();
        newArea.setParentId(0);
        List<NewArea> queryNewAreaList = newAreaService.queryNewAreaList(newArea);//省
        List<LeshuaProvince> list = new ArrayList<LeshuaProvince>();
        for (NewArea leshuaProvince : queryNewAreaList) {
            LeshuaProvince l = new LeshuaProvince();
            l.setCode(leshuaProvince.getId() + "");
            l.setName(leshuaProvince.getName());
            list.add(l);

        }
        for (LeshuaProvince leshuaProvince : list) {
            NewArea newArea1 = new NewArea();
            newArea1.setParentId(Integer.valueOf(leshuaProvince.getCode()));
            List<NewArea> queryNewAreaList2 = newAreaService.queryNewAreaList(newArea1);
            List<LeshuaCity> leshuaCities = new ArrayList<LeshuaCity>();
            for (NewArea leshuaProvince2 : queryNewAreaList2) {
                LeshuaCity leshuaCity = new LeshuaCity();
                leshuaCity.setCode(leshuaProvince2.getId() + "");
                leshuaCity.setName(leshuaProvince2.getName());
                List<LeshuaArea> areaList = new ArrayList<LeshuaArea>();
                NewArea newArea2 = new NewArea();
                newArea2.setId(leshuaProvince2.getId());
                List<NewArea> queryNewAreaList3 = newAreaService.queryNewAreaList(newArea2);
                for (NewArea leshuaProvince3 : queryNewAreaList3) {
                    LeshuaArea leshuaArea = new LeshuaArea();
                    leshuaArea.setCode(leshuaProvince3.getId() + "");
                    leshuaArea.setName(leshuaProvince3.getName());
                    areaList.add(leshuaArea);
                }
                leshuaCity.setAreaList(areaList);
                leshuaCities.add(leshuaCity);
            }
            leshuaProvince.setCitylist(leshuaCities);

        }
        return ResultGenerator.genSuccessResult(list);
    }


}

