package com.qs.mp.web.controller.api.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qs.mp.common.core.domain.AjaxResult;
import com.qs.mp.common.core.page.TableDataInfo;
import com.qs.mp.common.domain.Area;
import com.qs.mp.common.domain.vo.CityVO;
import com.qs.mp.common.domain.vo.ProvinceVO;
import com.qs.mp.common.service.IAreaService;
import com.qs.mp.web.controller.common.BaseApiController;

import java.util.ArrayList;
import java.util.List;
import javax.validation.Valid;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lgh on 2018/10/26.
 */

@RestController
@RequestMapping("/api/v1/mp")
@Api(tags = "区域管理接口")
public class AreaMgrController extends BaseApiController {

    @Autowired
    private IAreaService areaService;

    @PostMapping("/admin/area/tree/list")
    @ApiOperation("省市树状列表")
    @ApiResponses(
            @ApiResponse(code = 200, message = "成功", response = Area.class)
    )
    public AjaxResult treeList() {
        // 获取省
        List<Area> provinceList = areaService.list(new LambdaQueryWrapper<Area>().eq(Area::getParentId, 0));
        List<ProvinceVO> provinceVOList = new ArrayList<>();
        for (Area province : provinceList) {
            ProvinceVO provinceVO = new ProvinceVO();
            BeanUtils.copyProperties(province, provinceVO);
            // 获取市
            List<Area> cityList = areaService.list(new LambdaQueryWrapper<Area>().eq(Area::getParentId, province.getAreaId()));

            List<CityVO> cityVOList = new ArrayList<>();
            for (Area city : cityList) {
                CityVO cityVO = new CityVO();
                BeanUtils.copyProperties(city, cityVO);
                cityVOList.add(cityVO);
            }
            provinceVO.setCityList(cityVOList);
            provinceVOList.add(provinceVO);
        }
        return AjaxResult.success(provinceVOList);
    }


    /**
     * 分页获取
     */
    @GetMapping("/admin/area/list")
    @ApiOperation("分页获取")
    @ApiResponses(
            @ApiResponse(code = 200, message = "成功", response = Area.class)
    )
//    @PreAuthorize("@pms.hasPermission('admin:area:page')")
    public TableDataInfo page(Area area) {
        startPage();
        List<Area> areaList = areaService.list(new LambdaQueryWrapper<Area>());
        return getDataTable(areaList);
    }

    /**
     * 获取省市
     */
    @GetMapping("/list")
    @ApiOperation("获取省市")
    @ApiResponses(
            @ApiResponse(code = 200, message = "成功", response = Area.class)
    )
//    @PreAuthorize("@pms.hasPermission('admin:area:list')")
    public ResponseEntity<List<Area>> list(Area area) {
        List<Area> areas = areaService.list(new LambdaQueryWrapper<Area>()
                .like(area.getAreaName() != null, Area::getAreaName, area.getAreaName()));
        return ResponseEntity.ok(areas);
    }

    /**
     * 通过父级id获取区域列表
     */
    @GetMapping("/listByPid")
    @ApiOperation("通过父级id获取区域列表")
    @ApiResponses(
            @ApiResponse(code = 200, message = "成功", response = Area.class)
    )
    public ResponseEntity<List<Area>> listByPid(Long pid) {
        List<Area> list = areaService.listByPid(pid);
        return ResponseEntity.ok(list);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation("获取信息")
    @ApiResponses(
            @ApiResponse(code = 200, message = "成功", response = Area.class)
    )
//    @PreAuthorize("@pms.hasPermission('admin:area:info')")
    public ResponseEntity<Area> info(@PathVariable("id") Long id) {
        Area area = areaService.getById(id);
        return ResponseEntity.ok(area);
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation("保存")
    @ApiResponses(
            @ApiResponse(code = 200, message = "成功", response = Area.class)
    )
//    @PreAuthorize("@pms.hasPermission('admin:area:save')")
    public ResponseEntity<Void> save(@Valid @RequestBody Area area) {
        if (area.getParentId() != null) {
            Area parentArea = areaService.getById(area.getParentId());
            area.setLevel(parentArea.getLevel() + 1);
        }
        areaService.save(area);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改
     */
    @PutMapping
//    @PreAuthorize("@pms.hasPermission('admin:area:update')")
    public ResponseEntity<Void> update(@Valid @RequestBody Area area) {
        areaService.updateById(area);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
//    @PreAuthorize("@pms.hasPermission('admin:area:delete')")
    public ResponseEntity<Void> delete(@PathVariable Long id) {
        Area area = areaService.getById(id);
        areaService.removeById(id);
        return ResponseEntity.ok().build();
    }

}
