package com.gxwebsoft.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.*;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.entity.Manager;
import com.gxwebsoft.shop.param.ManagerParam;
import com.gxwebsoft.shop.service.ManagerService;
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 2022-11-25 14:02:24
 */
@Api(tags = "区域经理管理")
@RestController
@RequestMapping("/api/shop/manager")
public class ManagerController extends BaseController {
    @Resource
    private ManagerService managerService;

    @PreAuthorize("hasAuthority('shop:manager:list')")
    @OperationLog
    @ApiOperation("分页查询区域经理")
    @GetMapping("/page")
    public ApiResult<PageResult<Manager>> page(ManagerParam param) {
        PageParam<Manager, ManagerParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
//        return success(managerService.page(page, page.getWrapper()));
        // 使用关联查询
        return success(managerService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('shop:manager:list')")
    @OperationLog
    @ApiOperation("查询全部区域经理")
    @GetMapping()
    public ApiResult<List<Manager>> list(ManagerParam param) {
        PageParam<Manager, ManagerParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
//        return success(managerService.list(page.getOrderWrapper()));
        // 使用关联查询
        return success(managerService.listRel(param));
    }

    @PreAuthorize("hasAuthority('shop:manager:list')")
    @OperationLog
    @ApiOperation("根据id查询区域经理")
    @GetMapping("/{id}")
    public ApiResult<Manager> get(@PathVariable("id") Integer id) {
//        return success(managerService.getById(id));
        // 使用关联查询
        return success(managerService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('shop:manager:save')")
    @OperationLog
    @ApiOperation("添加区域经理")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Manager manager) {
        // 查询该地区是否已有区域经理
        LambdaQueryWrapper<Manager> wrapper = Wrappers.lambdaQuery(Manager.class)
                .eq(Manager::getProvince, manager.getProvince())
                .eq(Manager::getCity, manager.getCity())
                .eq(Manager::getArea, manager.getArea());
        int count = managerService.count(wrapper);
        if(count > 0){
            return fail("次区域已有区域经理");
        }
        // 记录当前登录用户id、租户id
        if (managerService.save(manager)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:manager:update')")
    @OperationLog
    @ApiOperation("修改区域经理")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Manager manager) {
        // 查询该地区是否已有区域经理
        LambdaQueryWrapper<Manager> wrapper = Wrappers.lambdaQuery(Manager.class)
                .eq(Manager::getProvince, manager.getProvince())
                .eq(Manager::getCity, manager.getCity())
                .eq(Manager::getArea, manager.getArea())
                .ne(Manager::getManagerId, manager.getManagerId());
        int count = managerService.count(wrapper);
        if(count > 0){
            return fail("此区域已有区域经理");
        }
        if (managerService.updateById(manager)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:manager:remove')")
    @OperationLog
    @ApiOperation("删除区域经理")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (managerService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('shop:manager:save')")
    @OperationLog
    @ApiOperation("批量添加区域经理")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Manager> list) {
        if (managerService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:manager:update')")
    @OperationLog
    @ApiOperation("批量修改区域经理")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Manager> batchParam) {
        if (batchParam.update(managerService, "manager_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:manager:remove')")
    @OperationLog
    @ApiOperation("批量删除区域经理")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (managerService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
