package com.gxwebsoft.apps.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.apps.service.CashierService;
import com.gxwebsoft.apps.entity.Cashier;
import com.gxwebsoft.apps.param.CashierParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
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;

/**
 * 海牛收银台记录表控制器
 *
 * @author WebSoft
 * @since 2022-11-18 11:47:09
 */
@Api(tags = "海牛收银台记录表管理")
@RestController
@RequestMapping("/api/apps/cashier")
public class CashierController extends BaseController {
    @Resource
    private CashierService cashierService;

    @PreAuthorize("hasAuthority('apps:cashier:list')")
    @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()));
        // 使用关联查询
        //return success(cashierService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('apps: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()));
        // 使用关联查询
        //return success(cashierService.listRel(param));
    }

    @PreAuthorize("hasAuthority('apps:cashier:list')")
    @OperationLog
    @ApiOperation("根据id查询海牛收银台记录表")
    @GetMapping("/{id}")
    public ApiResult<Cashier> get(@PathVariable("id") Integer id) {
        return success(cashierService.getById(id));
        // 使用关联查询
        //return success(cashierService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('apps:cashier:save')")
    @OperationLog
    @ApiOperation("添加海牛收银台记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Cashier cashier) {
        if(getMerchantCode() != null){
          cashier.setMerchantCode(getMerchantCode());
        }
        if (cashierService.save(cashier)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('apps:cashier:update')")
    @OperationLog
    @ApiOperation("修改海牛收银台记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Cashier cashier) {
        if (cashierService.updateById(cashier)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('apps:cashier:remove')")
    @OperationLog
    @ApiOperation("删除海牛收银台记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (cashierService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('apps:cashier:save')")
    @OperationLog
    @ApiOperation("批量添加海牛收银台记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Cashier> list) {
        if (cashierService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('apps:cashier:update')")
    @OperationLog
    @ApiOperation("批量修改海牛收银台记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Cashier> batchParam) {
        if (batchParam.update(cashierService, "cashier_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('apps:cashier:remove')")
    @OperationLog
    @ApiOperation("批量删除海牛收银台记录表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (cashierService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
