package io.hmit.modules.area.controller;

import io.hmit.common.annotation.LogOperation;
import io.hmit.common.constant.Constant;
import io.hmit.common.exception.ErrorCode;
import io.hmit.common.page.PageData;
import io.hmit.common.utils.ExcelUtils;
import io.hmit.common.utils.Result;
import io.hmit.common.validator.AssertUtils;
import io.hmit.common.validator.ValidatorUtils;
import io.hmit.common.validator.group.AddGroup;
import io.hmit.common.validator.group.DefaultGroup;
import io.hmit.common.validator.group.UpdateGroup;
import io.hmit.modules.area.dto.AreaDTO;
import io.hmit.modules.area.excel.AreaExcel;
import io.hmit.modules.area.service.AreaService;
import io.hmit.modules.user.dto.UserDTO;
import io.hmit.modules.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 区市管理
 *
 * @author syf syf0412@qq.com
 * @since 1.0.0 2020-12-25
 */
@RestController
@RequestMapping("area/area")
@Api(tags="区市管理")
public class AreaController {
    @Autowired
    private AreaService areaService;

    @Autowired
    private UserService userService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
     //@RequiresPermissions("area:area:page")
    public Result<PageData<AreaDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<AreaDTO> page = areaService.page(params);

        return new Result<PageData<AreaDTO>>().ok(page);
    }

    @GetMapping("list")
    @ApiOperation("列表")
    @ApiImplicitParam(name = "type", value = "菜单类型 0：市级 1：区级  null：全部", paramType = "query", dataType = "int")
    public Result<List<AreaDTO>> list(Integer type) {

        List<AreaDTO> list = areaService.getAllAreaList(type);

        return new Result<List<AreaDTO>>().ok(list);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
     //@RequiresPermissions("area:area:info")
    public Result<AreaDTO> get(@PathVariable("id") Long id){
        AreaDTO data = areaService.get(id);

        return new Result<AreaDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
     //@RequiresPermissions("area:area:save")
    public Result save(@RequestBody AreaDTO dto){
        //校验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        areaService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
     //@RequiresPermissions("area:area:update")
    public Result update(@RequestBody AreaDTO dto){
        //校验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        areaService.update(dto);

        return new Result();
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("area:area:delete")
    public Result delete(@PathVariable("id") Long id){
        //校验数据
        AssertUtils.isNull(id, "id");

        //判断是否有子菜单或按钮
        List<AreaDTO> list = areaService.getListPid(id);
        if (list.size() > 0) {
            return new Result().error(ErrorCode.SUB_MENU_EXIST);
        }
        Map<String, Object> params = new HashMap<>();
        params.put("area",id);
        List<UserDTO> userList = userService.list(params);
        if(userList.size()>0){
            return new Result().error(500,"当前地区存在会员信息，请核对后再进行删除");
        }
        areaService.delete(id);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
     //@RequiresPermissions("area:area:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<AreaDTO> list = areaService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, AreaExcel.class);
    }

}
