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

import com.bonus.canteen.core.common.base.BaseController;
import com.bonus.canteen.core.common.page.QueryParseHelper;
import com.bonus.canteen.core.customer.dto.AddPsnTypeDTO;
import com.bonus.canteen.core.customer.dto.DeletePsnTypeDTO;
import com.bonus.canteen.core.customer.dto.QueryPsnTypeDTO;
import com.bonus.canteen.core.customer.dto.UpdatePsnTypeDTO;
import com.bonus.canteen.core.customer.model.CustPsnType;
import com.bonus.canteen.core.customer.service.CustPsnTypeService;
import com.bonus.canteen.core.customer.v4.web.vo.ExportPsnTypeVO;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.web.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import static com.bonus.common.core.utils.PageUtils.startPage;
import static com.bonus.common.core.web.domain.AjaxResult.success;

@RestController
@RequestMapping({"/api/v1/cust/psntype"})
@Api(
   tags = {"customer-人员类别控制层"}
)
public class CustPsnTypeController extends BaseController {
   private static final Logger log = LoggerFactory.getLogger(CustPsnTypeController.class);
   private final CustPsnTypeService custPsnTypeService;

   @PostMapping({"/savePsnType"})
   @ApiOperation("新增人员类别信息")
   public AjaxResult savePsnType(@RequestBody @Valid AddPsnTypeDTO dto) {
      this.custPsnTypeService.addPsnType(dto);
      return success();
   }

   @GetMapping({"/queryPsnTypeByPage"})
   @ApiOperation("获取人员类别信息")
   public TableDataInfo queryPsnTypeByPage(@Valid QueryPsnTypeDTO dto) {
      try {
         startPage();
         List<ExportPsnTypeVO> list = this.custPsnTypeService.queryPsnTypeByPage(dto);
         return getDataTable(list);
      } catch (Exception e) {
         log.error(e.toString(), e);
         return getDataTable(null);
      }
   }

   @PutMapping({"/updatePsnTypeById"})
   @ApiOperation("更新人员类别")
   public AjaxResult updatePsnTypeById(@RequestBody @Valid UpdatePsnTypeDTO dto) {
      this.custPsnTypeService.updatePsnTypeById(dto);
      return success();
   }

   @DeleteMapping({"/deletePsnTypeById"})
   @ApiOperation("根据id删除人员类别")
   public AjaxResult deletePsnTypeById(@Valid Long id) {
      DeletePsnTypeDTO dto = new DeletePsnTypeDTO();
      dto.setId(id);
      this.custPsnTypeService.deletePsnTypeById(dto);
      return success();
   }

   @GetMapping({"/queryPsnTypeApplet"})
   @ApiOperation("获取人员类别信息")
   public AjaxResult queryPsnTypeApplet() {
      log.info("[人员类别]小程序获取人员类别信息");
      List<CustPsnType> result = this.custPsnTypeService.queryPsnTypeApplet();
      return success(result);
   }

   public CustPsnTypeController(final CustPsnTypeService custPsnTypeService) {
      this.custPsnTypeService = custPsnTypeService;
   }
}
