package com.gxwebsoft.apps.controller;

import com.gxwebsoft.apps.utils.BcUtil;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.apps.service.BcEquipmentService;
import com.gxwebsoft.apps.entity.BcEquipment;
import com.gxwebsoft.apps.param.BcEquipmentParam;
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 科技小王子
 * @since 2023-05-02 10:34:40
 */
@Api(tags = "报餐设备管理管理")
@RestController
@RequestMapping("/api/apps/bc-equipment")
public class BcEquipmentController extends BaseController {
    @Resource
    private BcEquipmentService bcEquipmentService;
    @Resource
    private BcUtil bcUtil;

    @PreAuthorize("hasAuthority('apps:bcEquipment:list')")
    @OperationLog
    @ApiOperation("分页查询报餐设备管理")
    @GetMapping("/page")
    public ApiResult<PageResult<BcEquipment>> page(BcEquipmentParam param) {
        PageParam<BcEquipment, BcEquipmentParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(bcEquipmentService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(bcEquipmentService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('apps:bcEquipment:list')")
    @OperationLog
    @ApiOperation("查询全部报餐设备管理")
    @GetMapping()
    public ApiResult<List<BcEquipment>> list(BcEquipmentParam param) {
        PageParam<BcEquipment, BcEquipmentParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(bcEquipmentService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(bcEquipmentService.listRel(param));
    }

    @PreAuthorize("hasAuthority('apps:bcEquipment:list')")
    @OperationLog
    @ApiOperation("根据id查询报餐设备管理")
    @GetMapping("/{id}")
    public ApiResult<BcEquipment> get(@PathVariable("id") Integer id) {
        return success(bcEquipmentService.getById(id));
        // 使用关联查询
        //return success(bcEquipmentService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('apps:bcEquipment:list')")
    @OperationLog
    @ApiOperation("添加报餐设备管理")
    @PostMapping()
    public ApiResult<?> save(@RequestBody BcEquipment bcEquipment) {
        // 记录当前登录用户id、租户id、商户编号
        User loginUser = getLoginUser();
        if (loginUser != null) {
          bcEquipment.setUserId(loginUser.getUserId());
          bcEquipment.setMerchantCode(getMerchantCode());
        }
        if (bcEquipmentService.save(bcEquipment)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('apps:bcEquipment:list')")
    @OperationLog
    @ApiOperation("修改报餐设备管理")
    @PutMapping()
    public ApiResult<?> update(@RequestBody BcEquipment bcEquipment) {
        if (bcEquipmentService.updateById(bcEquipment)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('apps:bcEquipment:list')")
    @OperationLog
    @ApiOperation("删除报餐设备管理")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (bcEquipmentService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('apps:bcEquipment:save')")
    @OperationLog
    @ApiOperation("批量添加报餐设备管理")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<BcEquipment> list) {
        if (bcEquipmentService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('apps:bcEquipment:update')")
    @OperationLog
    @ApiOperation("批量修改报餐设备管理")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<BcEquipment> batchParam) {
        if (batchParam.update(bcEquipmentService, "bc_equipment_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('apps:bcEquipment:remove')")
    @OperationLog
    @ApiOperation("批量删除报餐设备管理")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (bcEquipmentService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('apps:bcEquipment:list')")
    @ApiOperation("发送企业微信推送消息")
    @PostMapping("/addSend")
    public ApiResult<?> addSend(@RequestBody BcEquipment bcEquipment) {
      bcUtil.send(bcEquipment.getComments());
      return success("发送成功");
    }

}
