package com.ylx.web.controller.massage;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.ylx.common.core.domain.R;
import com.ylx.common.utils.uuid.IdUtils;
import com.ylx.massage.domain.TCollect;
import com.ylx.massage.service.TCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * 收藏表 前端控制器
 */
@RestController
@Slf4j
@RequestMapping("api/collect/v1")
@Api(tags = {"收藏"})
public class TCollectController {
    @Resource
    private TCollectService collectService;

    /**
     * 添加收藏
     *
     * @param collect
     * @return
     */
    @ApiOperation("添加收藏")
    @RequestMapping(value = "/wx/addCollect", method = RequestMethod.POST)
    public R addCollect(@RequestBody TCollect collect) {
        try {
            if (StringUtils.isBlank(collect.getcWxOpenId())) {
                return R.fail("openId不能为空");
            }
            if (StringUtils.isBlank(collect.getcJsId())) {
                return R.fail("技师Id不能为空");
            }
            TCollect collect1 = collectService.selectCollect(collect);
            if(collect1 !=null){
                return R.ok();
            }
            return R.ok(collectService.save(collect));
        } catch (Exception e) {
            log.error("收藏失败", e);
            return R.fail("收藏失败");
        }

    }

    /**
     * 获取收藏
     *
     * @param param
     * @return
     */
    @ApiOperation("获取收藏")
    @RequestMapping(value = "/wx/getCollect", method = RequestMethod.POST)
    public R<List<TCollect>> getCollect(@RequestBody TCollect param) {
        List<TCollect> list = collectService.getAll(param);
        return R.ok(list);
    }

    /**
     * 取消收藏
     *
     * @param param
     * @return
     */
    @ApiOperation("取消收藏")
    @RequestMapping(value = "/wx/delCollect", method = RequestMethod.POST)
    public R delCollect(@RequestBody TCollect param) {

        try {
            if (StringUtils.isNotBlank(param.getcId())) {
                collectService.removeById(param.getcId());
            } else if (StringUtils.isNotBlank(param.getcWxOpenId()) && StringUtils.isNotBlank(param.getcJsId())) {
                LambdaQueryWrapper<TCollect> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
                objectLambdaQueryWrapper.eq(TCollect::getcWxOpenId, param.getcWxOpenId()).eq(TCollect::getcJsId, param.getcJsId());
                collectService.remove(objectLambdaQueryWrapper);
            }
            return R.ok();
        } catch (Exception e) {
            log.error("取消收藏失败", e);
            return R.fail("取消收藏失败");
        }
    }
}
