package com.cyl.h5.controller;

import com.cyl.h5.config.SecurityUtil;
import com.cyl.h5.pojo.dto.ProductDTO;
import com.cyl.h5.service.H5MemberAddressService;
import com.cyl.manager.pms.domain.Product;
import com.cyl.manager.ums.domain.Member;
import com.cyl.manager.ums.domain.MemberAddress;
import com.cyl.manager.ums.domain.Pet;
import com.cyl.manager.ums.pojo.dto.MemberAddressDTO;
import com.cyl.manager.ums.pojo.query.MemberAddressQuery;
import com.cyl.manager.ums.pojo.query.PetQuery;
import com.cyl.manager.ums.pojo.vo.MemberAddressVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
//@Api("会员地址")
@RestController
@RequestMapping("/h5/member/address")
public class H5MemberAddressController {

  @Autowired
  private H5MemberAddressService h5MemberAddressService;

  //@ApiOperation("会员地址列表")
  @PostMapping("/list")
  public ResponseEntity<Page<MemberAddressVO>> getList(@RequestBody MemberAddressQuery query, Pageable page){
    Member member = SecurityUtil.getLocalMember();
    query.setMemberId(member.getId());
    List<MemberAddress> list = h5MemberAddressService.selectList(query,page);
    List<MemberAddressVO> result = new ArrayList<>();
    for (MemberAddress memberAddress : list) {
      MemberAddressVO memberAddressVO = new MemberAddressVO();
      BeanUtils.copyProperties(memberAddress, memberAddressVO);
      result.add(memberAddressVO);
    }
    return ResponseEntity.ok(new PageImpl<>(result, page, ((com.github.pagehelper.Page)list).getTotal()));
  }

  //@ApiOperation("获取会员默认地址")
  @GetMapping("/default")
  public ResponseEntity<MemberAddressVO> getDefault(){
    return ResponseEntity.ok(h5MemberAddressService.getDefault());
  }

  //@ApiOperation("创建会员地址")
  @PostMapping("/create")
  public ResponseEntity<Long> create(@RequestBody MemberAddressDTO memberAddressDTO){
    Member member = SecurityUtil.getLocalMember();
    memberAddressDTO.setMemberId(member.getId());
    return ResponseEntity.ok(h5MemberAddressService.insert(memberAddressDTO));
  }

  //@ApiOperation("更新会员地址")
  @PutMapping("/update")
  public ResponseEntity<Integer> update(@RequestBody MemberAddressDTO memberAddressDTO){
    Member member = SecurityUtil.getLocalMember();
    memberAddressDTO.setMemberId(member.getId());
    return ResponseEntity.ok(h5MemberAddressService.update(memberAddressDTO));
  }

  //@ApiOperation("获取会员地址详情")
  @GetMapping("/{id}")
  public ResponseEntity<MemberAddressVO> getInfo(@PathVariable Long id){
    return ResponseEntity.ok(h5MemberAddressService.selectById(id));
  }

  //@ApiOperation("删除会员地址")
  @DeleteMapping("/{id}")
  public ResponseEntity<Integer> remove(@PathVariable Long id) {
    return ResponseEntity.ok(h5MemberAddressService.deleteById(id));
  }
}
