package com.ylx.web.controller.massage;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylx.common.core.controller.BaseController;
import com.ylx.common.core.domain.R;
import com.ylx.massage.domain.TAddress;
import com.ylx.massage.service.TAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

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

/**
 * 轮播图(TAddress)表控制层
 *
 * @author makejava
 * @since 2024-04-11 17:18:52
 */
@RestController
@Api(tags = {"地址管理"})
@RequestMapping("tAddress")
@Slf4j
public class TAddressController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private TAddressService tAddressService;

    /**
     * 分页查询所有地址数据
     *
     * @param page     分页对象
     * @param tAddress 查询实体
     * @return R 所有数据
     */
    @GetMapping
    public R selectAll(Page<TAddress> page, TAddress tAddress) {
        return R.ok(this.tAddressService.page(page, new QueryWrapper<>(tAddress)));
    }


    /**
     * 根据姓名查询通讯地址列表
     *
     * @param page
     * @param tAddress
     * @return R
     */
    @GetMapping("pc/getAddressList")
    @ApiOperation("通讯地址列表")
    public R getAddressList(Page<TAddress> page, TAddress tAddress) {
        Page<TAddress> page1 = tAddressService.page(page, new LambdaQueryWrapper<TAddress>()
                .like(TAddress::getUserName, tAddress.getUserName())
                .orderByDesc(TAddress::getCreateTime));
        return R.ok(page1);
    }


    /**
     * 根据OpenId查询用户地址
     *
     * @param openId
     * @return R<List < TAddress>> 单条数据
     */
    @ApiOperation("根据OpenId查询用户地址")
    @GetMapping("getByOpenId")
    public R<List<TAddress>> selectOne(@RequestParam String openId) {
        LambdaQueryWrapper<TAddress> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
        objectLambdaQueryWrapper.eq(TAddress::getOpenid, openId);
        return R.ok(this.tAddressService.list(objectLambdaQueryWrapper));
    }

    @ApiOperation("根据Id查询地址")
    @GetMapping("getById")
    public R<TAddress> getById(@RequestParam String id) {
        return R.ok(this.tAddressService.getById(id));
    }


    /**
     * 根据手机号码查询技师的虚拟地址（PC端）
     *
     * @param phone
     * @return R<List<TAddress>>
     */
    @ApiOperation("根据手机号码查询技师的虚拟地址")
    @GetMapping("getVirtualAddressByPhone")
    public R<List<TAddress>> getVirtualAddressByPhone(@RequestParam String phone) {
        try {
            LambdaQueryWrapper<TAddress> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
            objectLambdaQueryWrapper.eq(TAddress::getPhone, phone).eq(TAddress::getType, 2).eq(TAddress::getUserType,2);
            return R.ok(this.tAddressService.list(objectLambdaQueryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }


    /**
     * 根据手机号码查询技师的虚拟地址（用户端）
     *
     * @param phone
     * @return R<List<TAddress>>
     */
    @ApiOperation("根据手机号码查询技师的虚拟地址")
    @GetMapping("/wx/getVirtualAddressByPhone")
    public R<List<TAddress>> getWXVirtualAddressByPhone(@RequestParam String phone) {
        try {
            LambdaQueryWrapper<TAddress> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
            objectLambdaQueryWrapper.eq(TAddress::getPhone, phone).eq(TAddress::getType, 2).eq(TAddress::getUserType,2);
            return R.ok(this.tAddressService.list(objectLambdaQueryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 新增地址（用户端）
     *
     * @param tAddress 实体对象
     * @return R 新增结果
     */
    @PostMapping("save")
    @ApiOperation("新增地址")
    public R insert(@RequestBody TAddress tAddress) {
        try {
            return R.ok(this.tAddressService.insertAddress(tAddress));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }


    /**
     * 新增虚拟地址（技师端）
     *
     * @param tAddress 实体对象
     * @return R 新增结果
     */
    @PostMapping("saveVirtualAddress")
    @ApiOperation("新增虚拟地址")
    public R saveVirtualAddress(@RequestBody TAddress tAddress) {
        try {
            log.info("tAddress参数的值：{}", JSON.toJSONString(tAddress));
            return R.ok(this.tAddressService.insertVirtualAddress(tAddress));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    /**
     * 修改数据
     *
     * @param tAddress 实体对象
     * @return 修改结果
     */
    @PostMapping("/update")
    @ApiOperation("修改地址")
    public R update(@RequestBody TAddress tAddress) {
        return R.ok(this.tAddressService.updateAddress(tAddress));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除地址")
    public R delete(@RequestParam("idList") List<Long> idList) {
        return R.ok(this.tAddressService.removeByIds(idList));
    }

    /**
     * 删除技师的虚拟地址
     *
     * @param id
     * @return R
     */
    @DeleteMapping("/deleteVirtualAddress")
    @ApiOperation("删除地址")
    public R deleteVirtualAddress(@RequestParam("id") String id) {
        try {
            return R.ok(this.tAddressService.removeById(id));
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }


    /**
     * 设置默认地址
     * @param tAddress
     * @return R
     */
    @PostMapping("/defaultAddress")
    @ApiOperation("设置默认地址")
    public R defaultAddress(@RequestBody TAddress tAddress) {
        return R.ok(this.tAddressService.defaultAddress(tAddress));
    }
}

