package org.jeecg.modules.alUser.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.address.entity.TbAddress;
import org.jeecg.modules.address.service.ITbAddressService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.postBean.AddressReq;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
* @Description: 用户地址表
* @Author: jeecg-boot
* @Date:   2024-10-19
* @Version: V1.0
*/
@Api(tags="用户地址")
@RestController
@RequestMapping("/address")
@Slf4j
public class AddressController extends JeecgController<TbAddress, ITbAddressService> {
   @Autowired
   private ITbAddressService tbAddressService;
   @Resource
   private ShiroRealm shiroRealm;
   /**
    * 分页列表查询
    *
    * @param
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   //@AutoLog(value = "用户地址表-分页列表查询")
   @ApiOperation(value="用户地址表-分页列表查询", notes="用户地址表-分页列表查询")
   @GetMapping(value = "/list")
   public Result<IPage<TbAddress>> queryPageList(
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       QueryWrapper<TbAddress> queryWrapper = QueryGenerator.initQueryWrapper(new TbAddress(), req.getParameterMap());
       queryWrapper.eq("is_disable",0).orderByDesc("create_time");
       Page<TbAddress> page = new Page<TbAddress>(pageNo, pageSize);
       IPage<TbAddress> pageList = tbAddressService.page(page, queryWrapper);
       return Result.OK(pageList);
   }

   /**
    *   添加
    *
    * @param tbAddress
    * @return
    */
   @ApiOperation(value="用户地址表-添加", notes="用户地址表-添加")
   @PostMapping(value = "/add")
   public Result<String> add(@RequestBody AddressReq tbAddress, @RequestHeader("X-Access-Token") String token) {
       HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);
       TbAddress tbAddress1 = new TbAddress();
       BeanUtils.copyProperties(tbAddress, tbAddress1);
       tbAddress1.setUserId(hanHaiMember.getId());
       tbAddressService.save(tbAddress1);
       return Result.OK("添加成功！");
   }

   /**
    *  编辑
    *
    * @param tbAddress
    * @return
    */
   @ApiOperation(value="用户地址表-编辑", notes="用户地址表-编辑")
   @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
   public Result<String> edit(@RequestBody AddressReq tbAddress) {
       TbAddress tbAddress1 = new TbAddress();
       BeanUtils.copyProperties(tbAddress, tbAddress1);
       tbAddressService.updateById(tbAddress1);
       return Result.OK("编辑成功!");
   }




}
