package com.gxwebsoft.love.controller;

import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.*;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.love.entity.Profit;
import com.gxwebsoft.love.param.ProfitParam;
import com.gxwebsoft.love.service.ProfitService;
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-06-28 20:49:17
 */
@Api(tags = "门店收益明细表管理")
@RestController
@RequestMapping("/api/love/profit")
public class ProfitController extends BaseController {
    @Resource
    private ProfitService profitService;

    @PreAuthorize("hasAuthority('love:profit:list')")
    @OperationLog
    @ApiOperation("分页查询门店收益明细表")
    @GetMapping("/page")
    public ApiResult<PageResult<Profit>> page(ProfitParam param) {
        // 使用关联查询
        return success(profitService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('love:profit:list')")
    @OperationLog
    @ApiOperation("查询全部门店收益明细表")
    @GetMapping()
    public ApiResult<List<Profit>> list(ProfitParam param) {
        // 使用关联查询
        return success(profitService.listRel(param));
    }

    @PreAuthorize("hasAuthority('love:profit:list')")
    @OperationLog
    @ApiOperation("根据id查询门店收益明细表")
    @GetMapping("/{id}")
    public ApiResult<Profit> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(profitService.getByIdRel(id));
    }

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

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

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

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

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

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

}
