package com.nantian.cloud.ggjg.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nantian.cloud.ggjg.entity.DoubleControl;
import com.nantian.cloud.ggjg.entity.Mlk;
import com.nantian.cloud.ggjg.entity.Quota;
import com.nantian.cloud.ggjg.service.DoubleControlService;
import com.nantian.cloud.ggjg.service.QuotaService;
import com.nantian.sc.cloud.common.core.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping("/quota")
@Api(value = "quota", tags = "定额")
public class QuotaController {

    private QuotaService quotaService;


    /**
     * 分页查询
     *
     * @param page             分页对象
     * @param quota
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分页获取定额", notes = "分页获取定额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "分页对象", required = false, dataType = "json", paramType = "path"),
            @ApiImplicitParam(name = "quota", value = "定额对象", required = false, dataType = "json", paramType = "path")
    })
    public R getQuotaPage(Page page, Quota quota) {
        return R.ok(quotaService.page(page, Wrappers.query(quota)));
    }


    /**
     * 信息
     *
     * @param quotaId
     * @return R
     */
    @GetMapping("/{quotaId}")
    @ApiOperation(value = "根据id获取定额", notes = "根据id获取定额")
    @ApiImplicitParam(name = "id", value = "id", required = true, dataType = "String", paramType = "path")
    public R getById(@PathVariable("quotaId") String quotaId) {
        return R.ok(quotaService.getById(quotaId));
    }

    /**
     * 保存
     *
     * @param quota
     * @return R
     */
    @PostMapping
    @ApiOperation(value = "新增定额", notes = "新增定额")
    @ApiImplicitParam(name = "quota", value = "定额对象", required = true, dataType = "json", paramType = "path")
    public R save(@RequestBody Quota quota) {
        Quota n = new Quota();
        n.setAreaCode(quota.getAreaCode());
        if(quotaService.getOne(Wrappers.query(n))!=null){
          return R.failed("记录已经存在不能重复添加！");
        }
        return R.ok(quotaService.save(quota));
    }

    /**
     * 修改
     *
     * @param quota
     * @return R
     */
    @PutMapping
    @ApiOperation(value = "修改先进定额", notes = "修改定额")
    @ApiImplicitParam(name = "quota", value = "定额对象", required = true, dataType = "json", paramType = "path")
    public R updateById(@RequestBody Quota quota) {
        return R.ok(quotaService.updateById(quota));
    }

    /**
     * 删除
     *
     * @param quotaId
     * @return R
     */
    @DeleteMapping("/{quotaId}")
    @ApiOperation(value = "根据id删除定额", notes = "根据id定额")
    @ApiImplicitParam(name = "quotaId", value = "quotaId", required = true, dataType = "String", paramType = "path")
    public R removeById(@PathVariable String quotaId) {
        return R.ok(quotaService.removeById(quotaId));
    }

    @GetMapping("/queryList")
    @ApiOperation(value = "定额列表", notes = "定额列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "quota", value = "定额对象", required = false, dataType = "json", paramType = "path")
    })
    public R queryList(Quota quota) {
        HashMap<String,String> map =new HashMap<String,String>();
        map.put("areaCode",quota.getAreaCode());
        map.put("type",quota.getType());
        return R.ok(quotaService.queryList(map));
    }

    @GetMapping("/quotaTjb")
    @ApiOperation(value = "定额", notes = "获取定额统计表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "quota", value = "定额对象", required = false, dataType = "json", paramType = "path")
    })
    public R getQuotaTjb(Quota quota) {
        HashMap<String,String> map =new HashMap<String,String>();
        map.put("areaCode",quota.getAreaCode());
        map.put("type",quota.getType());
        return R.ok(quotaService.getQuotaTjb(map));
    }

    @GetMapping("/overstretch")
    @ApiOperation(value = "分区域、机构类型获取超额数据", notes = "分区域、机构类型获取超额数据")
    @ApiImplicitParam(name = "quota", value = "定额对象", required = false, dataType = "json", paramType = "path")
    public R getOverstretch(Quota quota) {
        if(quota.getAreaCode()==null || "".equals(quota.getAreaCode())) {
            return R.failed("地区码 areaCode 不能为空！");
        }
        return R.ok(quotaService.getOverstretch(quota));
    }

}
