package cn.pluss.platform.controller.address;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.entity.UserAddress;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.userAddress.UserAddressService;
import cn.pluss.platform.userApp.UserAppService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * TODO
 * 用户地址控制器
 * @author crystal
 * @date 2022/3/24 17:44
 */
@RestController
@RequestMapping(value = "address")
public class UserAddressController {

    @Resource
    private UserAddressService userAddressService;

    @Resource
    private UserAppService userAppService;

    @GetMapping(value = "/list")
    @ApiOperation(tags = {"页面-设备管理-地址管理"}, value = "查询用户收货地址列表", notes = "查询用户收货地址列表", httpMethod = "GET")
    public Result<List<UserAddress>> list()  {
        UserApp userApp = userAppService.queryUserAppByToken();
        if(userApp == null){
            return ResultGenerator.genFailResult("登录用户异常");
        }
        List<UserAddress> list = userAddressService.getAddressList(userApp.getUserId(), UserAddress.ADDRESS_TYPE_COLLECT, false);
        return ResultGenerator.genSuccessResult(list);
    }

    @GetMapping(value = "/get/{id}")
    @ApiOperation(tags = {"页面-设备管理-地址管理"}, value = "查询用户收货地址详情", notes = "查询用户收货地址详情", httpMethod = "GET")
    public Result<UserAddress> get(@PathVariable("id") Long id)  {
        UserAddress address = userAddressService.getById(id);
        return ResultGenerator.genSuccessResult(address);
    }

    @GetMapping(value = "/defaultOne")
    @ApiOperation(tags = {"页面-设备管理-地址管理"}, value = "查询用户收货地址详情", notes = "查询用户收货地址详情", httpMethod = "GET")
    public Result<UserAddress> defaultOne()  {
        UserApp userApp = userAppService.queryUserAppByToken();
        LambdaQueryWrapper<UserAddress> qWrapper = Wrappers.lambdaQuery();
        qWrapper.eq(UserAddress::getUserId, userApp.getUserId());
        qWrapper.orderByDesc(UserAddress::getDefFlag);
        qWrapper.orderByAsc(UserAddress::getId).last("limit 1");
        UserAddress address = userAddressService.getOne(qWrapper);

        return ResultGenerator.genSuccessResult(address);
    }

    @RequestMapping(value = "/delete")
    @ApiOperation(tags = {"页面-设备管理-地址管理"}, value = "删除收货地址", notes = "删除收货地址", httpMethod = "GET")
    public Result<Object> delete(@RequestParam("ids[]") Long[] ids)  {
        if(ids == null || ids.length == 0){
            return ResultGenerator.genSuccessResult("缺失ids参数",null);
        }
        userAddressService.removeByIds(Arrays.asList(ids));
        return ResultGenerator.genSuccessResult("删除成功",null);
    }

    @PostMapping(value = "/save")
    @ApiOperation(tags = {"页面-设备管理-地址管理"}, value = "保存收货地址", notes = "保存收货地址", httpMethod = "POST")
    public Result<Object> save(@RequestBody UserAddress userAddress)  {
        UserApp userApp = userAppService.queryUserAppByToken();
        if(userApp == null){
            return ResultGenerator.genFailResult("登录用户异常");
        }
        userAddress.setUserId(Integer.parseInt(userApp.getUserId().toString()));
        userAddressService.saveOrUpdate(userAddress);
        return ResultGenerator.genSuccessResult("操作成功",null);
    }
}
