package com.eleadmin.site.controller;

import com.eleadmin.common.core.annotation.OperationLog;
import com.eleadmin.common.core.web.ApiResult;
import com.eleadmin.common.core.web.BaseController;
import com.eleadmin.common.core.web.PageParam;
import com.eleadmin.common.core.web.PageResult;
import com.eleadmin.site.entity.Cashier;
import com.eleadmin.site.param.CashierParam;
import com.eleadmin.site.server.CashierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 收银台控制器
 *
 * @author gxwebsoft.com
 * @since 2018-12-24 16:10:11
 */
@Api(tags = "收银台")
@RestController
@RequestMapping("/api/cashier")
public class CashierController extends BaseController {
  @Resource
  private CashierService cashierService;


  @OperationLog
  @ApiOperation("分页查询订单")
  @GetMapping("/page")
  public ApiResult<PageResult<Cashier>> page(CashierParam param) {
    PageParam<Cashier, CashierParam> page = new PageParam<>(param);
    page.setDefaultOrder("create_time desc");
    return success(cashierService.page(page, page.getWrapper()));
  }

  @PreAuthorize("hasAuthority('sys:cashier:list')")
  @OperationLog
  @ApiOperation("查询全部订单")
  @GetMapping()
  public ApiResult<List<Cashier>> list(CashierParam param) {
    PageParam<Cashier, CashierParam> page = new PageParam<>(param);
    page.setDefaultOrder("create_time desc");
    return success(cashierService.list(page.getOrderWrapper()));
  }

  @PreAuthorize("hasAuthority('sys:cashier:list')")
  @OperationLog
  @ApiOperation("根据id查询订单")
  @GetMapping("/{id}")
  public ApiResult<Cashier> get(@PathVariable("id") Integer id) {
    return success(cashierService.getById(id));
  }


  @ApiOperation("添加订单")
  @PostMapping()
  public ApiResult<?> add(@RequestBody Cashier customer) {
    // 签名校验
    if(!Objects.equals(customer.getSign(), "dk9mci8mcQ5Wa4xWRiojEjI6IibNJCLvl2cyVmdiwiIgwQ0NRNkICL0kjNIzojI0NWZqJWd6ICZpJISOuRpnM1JyetRWQlf0NW==")){
      return fail("签名失败");
    }
    // IP地址校验

    if (cashierService.save(customer)) {
      return success("添加成功");
    }
    return fail("添加失败");
  }

  @PreAuthorize("hasAuthority('sys:cashier:update')")
  @OperationLog
  @ApiOperation("修改订单")
  @PutMapping()
  public ApiResult<?> update(@RequestBody Cashier customer) {
    if (cashierService.updateById(customer)) {
      return success("修改成功");
    }
    return fail("修改失败");
  }

  @PreAuthorize("hasAuthority('sys:cashier:remove')")
  @OperationLog
  @ApiOperation("删除订单")
  @DeleteMapping("/{id}")
  public ApiResult<?> remove(@PathVariable("id") Integer id) {
    if (cashierService.removeById(id)) {
      return success("删除成功");
    }
    return fail("删除失败");
  }

  @PreAuthorize("hasAuthority('sys:cashier:save')")
  @OperationLog
  @ApiOperation("批量添加订单")
  @PostMapping("/batch")
  public ApiResult<List<String>> saveBatch(@RequestBody List<Cashier> list) {
    if (cashierService.saveBatch(list)) {
      return success("添加成功", null);
    }
    return fail("添加失败", null);
  }

  @PreAuthorize("hasAuthority('sys:cashier:remove')")
  @OperationLog
  @ApiOperation("批量删除订单")
  @DeleteMapping("/batch")
  public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
    if (cashierService.removeByIds(ids)) {
      return success("删除成功");
    }
    return fail("删除失败");
  }
}
