package com.gxwebsoft.shop.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.service.MemberService;
import com.gxwebsoft.shop.entity.Member;
import com.gxwebsoft.shop.param.MemberParam;
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 2022-11-25 14:02:24
 */
@Api(tags = "商城会员管理")
@RestController
@RequestMapping("/api/shop/member")
public class MemberController extends BaseController {
    @Resource
    private MemberService memberService;

    @PreAuthorize("hasAuthority('shop:member:list')")
    @OperationLog
    @ApiOperation("分页查询商城会员")
    @GetMapping("/page")
    public ApiResult<PageResult<Member>> page(MemberParam param) {
        PageParam<Member, MemberParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
//        return success(memberService.page(page, page.getWrapper()));
        // 使用关联查询
        return success(memberService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('shop:member:list')")
    @OperationLog
    @ApiOperation("查询全部商城会员")
    @GetMapping()
    public ApiResult<List<Member>> list(MemberParam param) {
        PageParam<Member, MemberParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
//        return success(memberService.list(page.getOrderWrapper()));
        // 使用关联查询
        return success(memberService.listRel(param));
    }

    @PreAuthorize("hasAuthority('shop:member:list')")
    @OperationLog
    @ApiOperation("根据id查询商城会员")
    @GetMapping("/{id}")
    public ApiResult<Member> get(@PathVariable("id") Integer id) {
        return success(memberService.getById(id));
        // 使用关联查询
        //return success(memberService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('shop:member:save')")
    @OperationLog
    @ApiOperation("添加商城会员")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Member member) {
        // 记录当前登录用户id、租户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          member.setUserId(loginUser.getUserId());
        }
        if (memberService.save(member)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:member:update')")
    @OperationLog
    @ApiOperation("修改商城会员")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Member member) {
        if (memberService.updateById(member)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:member:remove')")
    @OperationLog
    @ApiOperation("删除商城会员")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (memberService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('shop:member:save')")
    @OperationLog
    @ApiOperation("批量添加商城会员")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Member> list) {
        if (memberService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:member:update')")
    @OperationLog
    @ApiOperation("批量修改商城会员")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Member> batchParam) {
        if (batchParam.update(memberService, "member_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:member:remove')")
    @OperationLog
    @ApiOperation("批量删除商城会员")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (memberService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
