package com.iformall.controller.basic;

import com.github.pagehelper.PageInfo;
import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.ErrorCode;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.dto.WxMallBuildingFloorDto;
import com.iformall.domain.po.WxMallBuilding;
import com.iformall.domain.po.WxMallFloor;
import com.iformall.service.WxMallBuildingService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("wxMallBuilding")
public class WxMallBuildingController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxMallBuildingService wxMallBuildingService;

    @ApiOperation("分页列表接口")
    @GetMapping("list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    @SystemControllerLog(description = "商城-楼座-列表")
    public ResultData list(@ModelAttribute WxMallBuilding wxMallBuilding, Integer pageNum, Integer pageSize) {
        logger.debug("[" + getIpAddr() + "] WxMallBuildingController::list");
        if (null == wxMallBuilding) wxMallBuilding = new WxMallBuilding();
        final PageInfo<WxMallBuilding> page = wxMallBuildingService.listAsPage(wxMallBuilding, pageNum, pageSize);
        return new ResultData(page);
    }

    @ApiOperation("新增接口")
    @PostMapping("add")
    @SystemControllerLog(description = "商城-楼座-新增")
    public ResultData add(@RequestBody WxMallBuilding wxMallBuilding) {
        logger.debug("[" + getIpAddr() + "] WxMallBuildingController::add");
        //Assert.notNull(wxMallBuilding.getName(), "角色名不能为空");
        //Assert.isTrue(!checkUnique(sysRole.getName(), null), "重复的角色名");
        wxMallBuildingService.saveOrUpdate(wxMallBuilding);
        return new ResultData();
    }

    @ApiOperation("根据id更新接口")
    @PostMapping("update")
    @SystemControllerLog(description = "商城-楼座-更新")
    public ResultData update(@RequestBody WxMallBuilding wxMallBuilding) {
        logger.debug("[" + getIpAddr() + "] WxMallBuildingController::update");
        wxMallBuildingService.saveOrUpdate(wxMallBuilding);
        return new ResultData();
    }

    @ApiOperation("根据id删除接口")
    @GetMapping("/del")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    @SystemControllerLog(description = "商城-楼座-删除")
    public ResultData delete(Long id) {
        logger.debug("[" + getIpAddr() + "] WxMallBuildingController::delete");
        wxMallBuildingService.deleteById(id);
        return new ResultData(Result.SUCCESS, "删除成功", null);
    }

    @ApiOperation("根据id查询接口")
    @GetMapping("/findById")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    @SystemControllerLog(description = "商城-楼座-查询")
    public ResultData findById(Long id) {
        logger.debug("[" + getIpAddr() + "] WxMallBuildingController::findById");
        return new ResultData(Result.SUCCESS, "查询成功", wxMallBuildingService.getById(id));
    }


    @ApiOperation("获取所有数据")
    @GetMapping("getbuildinglist")
    @SystemControllerLog(description = "商城-楼座-获取所有数据")
    public ResultData getbuildinglist() {
        logger.debug("[" + getIpAddr() + "] WxMallBuildingController::getbuildinglist");
        return wxMallBuildingService.getbuildinglist(getTenantId());
    }

    @ApiOperation("获取楼层楼座数据")
    @GetMapping("getbuildingfloorlist")
    @SystemControllerLog(description = "商城-楼座-获取楼层楼座数据")
    public ResultData getbuildingfloorlist() {
        logger.debug("[" + getIpAddr() + "] WxMallBuildingController::getbuildingfloorlist");
        return wxMallBuildingService.getbuildingfloorlist(getTenantId());
    }

    @ApiOperation("新增接口(3)")
    @PostMapping("addBuildingAndFloor")
    @SystemControllerLog(description = "商城-楼座/楼层-新增")
    public ResultData addBuildingAndFloor(@RequestBody WxMallBuildingFloorDto mallBuildingFloorDto) {
        logger.debug("[" + getIpAddr() + "] WxMallBuildingController::addBuildingAndFloor");
        if(mallBuildingFloorDto.getMallId() == null || mallBuildingFloorDto.getMallId() == 0) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);
        }
        if(StringUtils.isBlank(mallBuildingFloorDto.getTenantId())) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);
        }
        if(StringUtils.isBlank(mallBuildingFloorDto.getBuilding())) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);
        }
        if(StringUtils.isBlank(mallBuildingFloorDto.getFloors())) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);
        }
        wxMallBuildingService.addBuildingAndFloors(mallBuildingFloorDto);
        return new ResultData();
    }

    @ApiOperation("保存楼层楼座面积")
    @PostMapping("saveFloorArea")
    @SystemControllerLog(description = "商城-楼座/楼层-保存面积")
    public ResultData saveFloorArea(@RequestBody WxMallFloor record) {
        logger.debug("[" + getIpAddr() + "] WxMallBuildingController::addBuildingAndFloor");
        if(record == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);
        }
        if (record.getId() == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);
        }
        if (record.getTotalArea() == null && record.getOperatingArea() == null) {
            return new ResultData(ErrorCode.SYS_PARAMETER_NOT_NULL);
        }
        record.setTenantId(getTenantId());
        wxMallBuildingService.saveFloorArea(record);
        return new ResultData();
    }


}
