package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.service.ProfitLogService;
import com.gxwebsoft.shop.entity.ProfitLog;
import com.gxwebsoft.shop.param.ProfitLogParam;
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 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 科技小王子
 * @since 2023-08-10 11:24:02
 */
@Api(tags = "门店收益明细表管理")
@RestController
@RequestMapping("/api/shop/profit-log")
public class ProfitLogController extends BaseController {
    @Resource
    private ProfitLogService profitLogService;

    @OperationLog
    @ApiOperation("分页查询门店收益明细表")
    @GetMapping("/page")
    public ApiResult<PageResult<ProfitLog>> page(ProfitLogParam param) {
        PageParam<ProfitLog, ProfitLogParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(profitLogService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(profitLogService.pageRel(param));
    }

    @OperationLog
    @ApiOperation("查询全部门店收益明细表")
    @GetMapping()
    public ApiResult<List<ProfitLog>> list(ProfitLogParam param) {
        PageParam<ProfitLog, ProfitLogParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(profitLogService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(profitLogService.listRel(param));
    }

    @OperationLog
    @ApiOperation("根据id查询门店收益明细表")
    @GetMapping("/{id}")
    public ApiResult<ProfitLog> get(@PathVariable("id") Integer id) {
        return success(profitLogService.getById(id));
        // 使用关联查询
        //return success(profitLogService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('shop:profitLog:save')")
    @OperationLog
    @ApiOperation("添加门店收益明细表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody ProfitLog profitLog) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          profitLog.setUserId(loginUser.getUserId());
        }
        if (profitLogService.save(profitLog)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:profitLog:update')")
    @OperationLog
    @ApiOperation("修改门店收益明细表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody ProfitLog profitLog) {
        if (profitLogService.updateById(profitLog)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:profitLog:remove')")
    @OperationLog
    @ApiOperation("删除门店收益明细表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (profitLogService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('shop:profitLog:save')")
    @OperationLog
    @ApiOperation("批量添加门店收益明细表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<ProfitLog> list) {
        if (profitLogService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:profitLog:update')")
    @OperationLog
    @ApiOperation("批量修改门店收益明细表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<ProfitLog> batchParam) {
        if (batchParam.update(profitLogService, "profit_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:profitLog:remove')")
    @OperationLog
    @ApiOperation("批量删除门店收益明细表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (profitLogService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
