package com.gxwebsoft.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.security.JwtUtil;
import com.gxwebsoft.common.core.utils.RequestUtil;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.mapper.MerchantAccountMapper;
import com.gxwebsoft.shop.service.MerchantAccountService;
import com.gxwebsoft.shop.entity.MerchantAccount;
import com.gxwebsoft.shop.param.MerchantAccountParam;
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 javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 商户账号控制器
 *
 * @author 科技小王子
 * @since 2024-04-19 12:02:24
 */
@Api(tags = "商户账号管理")
@RestController
@RequestMapping("/api/shop/merchant-account")
public class MerchantAccountController extends BaseController {
    @Resource
    private MerchantAccountService merchantAccountService;
    @Resource
    private MerchantAccountMapper merchantAccountMapper;

    @ApiOperation("分页查询商户账号")
    @GetMapping("/page")
    public ApiResult<PageResult<MerchantAccount>> page(MerchantAccountParam param) {
        // 使用关联查询
        return success(merchantAccountService.pageRel(param));
    }

    @ApiOperation("查询全部商户账号")
    @GetMapping()
    public ApiResult<List<MerchantAccount>> list(MerchantAccountParam param) {
        // 使用关联查询
        return success(merchantAccountService.listRel(param));
    }

    @ApiOperation("根据id查询商户账号")
    @GetMapping("/{id}")
    public ApiResult<MerchantAccount> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(merchantAccountService.getByIdRel(id));
    }

    @ApiOperation("添加商户账号")
    @PostMapping()
    public ApiResult<?> save(@RequestBody MerchantAccount merchantAccount, HttpServletRequest request) {
        if (merchantAccountService.count(new LambdaQueryWrapper<MerchantAccount>().eq(MerchantAccount::getPhone,merchantAccount.getPhone())) > 0) {
          return fail("手机号码已存在");
        }
        // 获取远程用户信息
        final RequestUtil requestUtil = new RequestUtil();
        String access_token = JwtUtil.getAccessToken(request);
        requestUtil.setAccessToken(access_token);
        requestUtil.setTenantId(getTenantId().toString());
        User userByPhone = requestUtil.getUserByPhone(merchantAccount.getPhone());
        // 新增注册
        if (userByPhone == null) {
          if (requestUtil.saveUserByPhone(merchantAccount)) {
            userByPhone = requestUtil.getUserByPhone(merchantAccount.getPhone());
          }
        }
        merchantAccount.setUserId(userByPhone.getUserId());
        if (merchantAccountService.save(merchantAccount)) {
          return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改商户账号")
    @PutMapping()
    public ApiResult<?> update(@RequestBody MerchantAccount merchantAccount) {
        if (merchantAccountService.updateById(merchantAccount)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("删除商户账号")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (merchantAccountService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("批量添加商户账号")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<MerchantAccount> list) {
        if (merchantAccountService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("批量修改商户账号")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<MerchantAccount> batchParam) {
        if (batchParam.update(merchantAccountService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("批量删除商户账号")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (merchantAccountService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("根据手机号码查询商户账号信息")
    @GetMapping("/getMerchantAccountByPhone")
    public ApiResult<MerchantAccount> getMerchantAccountByPhone(MerchantAccountParam param){
      param.setTenantId(getTenantId());
      final MerchantAccount merchantAccount = merchantAccountMapper.getMerchantAccountByPhone(param);
      if(merchantAccount == null){
        return fail("账号不存在",null);
      }
      return success(merchantAccount);
    }

}
