package com.yiqi.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yiqi.common.exception.ServiceException;
import com.yiqi.app.domain.AppUserAddress;
import com.yiqi.app.domain.dto.AppUserAddressDTO;
import com.yiqi.app.service.IAppUserAddressService;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.domain.AuthHolder;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.enums.SysBoolType;
import com.yiqi.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author xiaohei
 * @date 2023/10/4
 */
@Api(tags = "我的地址")
@RestController
@RequestMapping(value = UrlConstants.wxappApi + "/address")
public class WeAppUserAddressController {

    @Autowired
    private IAppUserAddressService appUserAddressService;

    @ApiOperation("获取我的地址列表")
    @GetMapping(value = "list")
    public R<List<AppUserAddress>> list() throws Exception{
        return R.ok(appUserAddressService.list(new QueryWrapper<AppUserAddress>().lambda()
                .eq(AppUserAddress::getAppUserId, AuthHolder.userId())
                .orderByDesc(AppUserAddress::getIsDefault)));
    }

    @ApiOperation("根据ID获取地址明细")
    @GetMapping(value = "getInfo")
    public R getInfo(@RequestParam Long id) throws Exception{
        return R.ok(appUserAddressService.getOne(new QueryWrapper<AppUserAddress>().lambda()
                .eq(AppUserAddress::getId,id)
                .eq(AppUserAddress::getAppUserId,AuthHolder.userId())));
    }

    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("新增地址")
    @PostMapping(value = "add")
    public R add(@RequestBody @Valid AppUserAddressDTO appUserAddressDTO) throws Exception{
        if(SysBoolType.YES.getCode().equals(appUserAddressDTO.getIsDefault())){
            appUserAddressService.update(new UpdateWrapper<AppUserAddress>()
                    .lambda().set(AppUserAddress::getIsDefault,SysBoolType.NO.getCode())
                    .eq(AppUserAddress::getAppUserId,AuthHolder.userId()));
        }
        AppUserAddress appUserAddress = new AppUserAddress();
        BeanUtils.copyProperties(appUserAddressDTO,appUserAddress,"id");
        appUserAddress.setCreateTime(DateUtils.getNowDate());
        appUserAddress.setAppUserId(AuthHolder.userId());
        return R.ok(appUserAddressService.save(appUserAddress));
    }

    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("修改地址")
    @PostMapping(value = "update")
    public R update(@RequestBody @Valid AppUserAddressDTO appUserAddressDTO) throws Exception{
        if(appUserAddressDTO.getId() == null){
            throw new ServiceException("ID不能为空");
        }
        if(SysBoolType.YES.getCode().equals(appUserAddressDTO.getIsDefault())){
            appUserAddressService.update(new UpdateWrapper<AppUserAddress>()
                    .lambda().set(AppUserAddress::getIsDefault,SysBoolType.NO.getCode())
                    .eq(AppUserAddress::getAppUserId,AuthHolder.userId())
                    .ne(AppUserAddress::getId,appUserAddressDTO.getId()));
        }

        AppUserAddress appUserAddress = appUserAddressService.getOne(new QueryWrapper<AppUserAddress>().lambda()
                .eq(AppUserAddress::getId,appUserAddressDTO.getId())
                .eq(AppUserAddress::getAppUserId,AuthHolder.userId()),false);
        if(appUserAddress == null){
            throw new ServiceException("未查询到地址");
        }

        BeanUtils.copyProperties(appUserAddressDTO,appUserAddress);
        appUserAddressService.updateById(appUserAddress);
        return R.ok();
    }

    @ApiOperation("删除地址")
    @GetMapping(value = "delete")
    public R delete(@RequestParam Long id) throws Exception{
        return R.ok(appUserAddressService.remove(new QueryWrapper<AppUserAddress>().lambda()
                .eq(AppUserAddress::getAppUserId,AuthHolder.userId())
                .eq(AppUserAddress::getId,id)));
    }

    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("设置默认地址")
    @GetMapping("setDefault")
    public R setDefault(@RequestParam @ApiParam("地址ID") Long id) throws Exception{
        appUserAddressService.update(new UpdateWrapper<AppUserAddress>().lambda()
                .eq(AppUserAddress::getAppUserId,AuthHolder.userId())
                .eq(AppUserAddress::getId,id)
                .set(AppUserAddress::getIsDefault,SysBoolType.YES.getCode()));
        appUserAddressService.update(new UpdateWrapper<AppUserAddress>().lambda()
                .eq(AppUserAddress::getAppUserId,AuthHolder.userId())
                .ne(AppUserAddress::getId,id)
                .set(AppUserAddress::getIsDefault,SysBoolType.NO.getCode()));
        return R.ok();
    }

}
