package com.bonus.canteen.core.drp.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bonus.canteen.core.drp.dto.ConversionUnitDTO;
import com.bonus.canteen.core.drp.dto.DrpUnitAddDTO;
import com.bonus.canteen.core.drp.dto.DrpUnitEditDTO;
import com.bonus.canteen.core.drp.dto.DrpUnitPageDTO;
import com.bonus.canteen.core.drp.service.DrpUnitService;
import com.bonus.canteen.core.drp.vo.DrpUnitPageVO;
import com.bonus.canteen.core.secure.filter.annotation.RequiresAuthentication;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.houqin.encrypt.RequiresGuest;
import com.bonus.common.security.annotation.RequiresPermissions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;

@RestController
@RequestMapping({"/api/v1/drpunit"})
@Api(
   value = "gyp_计量单位管理",
   tags = {"gyp_计量单位管理"}
)
public class DrpUnitController {
   private static final Logger log = LoggerFactory.getLogger(DrpUnitController.class);
   @Resource
   @Lazy
   private DrpUnitService drpUnitService;

   @RequiresPermissions({"drp:unit:insert"})
   @PostMapping({"/add"})
   @RequiresAuthentication
   @ApiOperation("新增计量单位")
   public AjaxResult addDrpUnit(@RequestBody @Valid DrpUnitAddDTO dto) {
      this.drpUnitService.addDrpUnit(dto);
      return AjaxResult.success();
   }

   @PostMapping({"/page"})
   @RequiresAuthentication
   @ApiOperation("分页获取计量单位")
   public AjaxResult getDrpUnitPage(@RequestBody DrpUnitPageDTO dto) {
      Page<DrpUnitPageVO> resultPage = this.drpUnitService.getDrpUnitPage(dto);
      return AjaxResult.success(resultPage);
   }

   @RequiresPermissions({"drp:unit:update"})
   @PostMapping({"/edit"})
   @RequiresAuthentication
   @ApiOperation("修改计量单位")
   public AjaxResult editDrpUnit(@RequestBody @Valid DrpUnitEditDTO dto) {
      this.drpUnitService.editDrpUnit(dto);
      return AjaxResult.success();
   }

   @RequiresPermissions({"drp:unit:delete"})
   @PostMapping({"/remove/{unitId}"})
   @RequiresGuest
   @ApiOperation("删除计量单位")
   public AjaxResult removeByUnitId(@PathVariable Long unitId) {
      this.drpUnitService.removeByUnitId(unitId);
      return AjaxResult.success();
   }

   @PostMapping({"/conversion/unit"})
   @RequiresGuest
   @ApiOperation("等价换算计量单位")
   public AjaxResult conversionUnit(@RequestBody @Valid ConversionUnitDTO dto) {
      BigDecimal bigDecimal = this.drpUnitService.conversionUnit(dto);
      return AjaxResult.success(bigDecimal);
   }
}
