package com.bjtdba.userCenter.controller;


import com.bjtdba.entity.userCenter.UserRelation;
import com.bjtdba.userCenter.service.UserRelationService;
import com.bjtdba.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("userCenter_service/relation")
public class UserRelationController {

    @Autowired
    private UserRelationService userRelationService;




    /**
     * 验证是否关注
     */

    @PostMapping("/query")
    public Object query(@RequestHeader Map<String,String> Hmap, @RequestBody Map<String,String> map){
        if(!Hmap.containsKey("uid")){
            return ResponseUtil.fail("uid 参数错误");
        }

        if(!map.containsKey("type")||!map.containsKey("type_id")){
            return ResponseUtil.fail("参数错误");
        }
        Integer uid =Integer.parseInt(Hmap.get("uid"));
        Integer type=Integer.parseInt(map.get("type"));
        Integer type_id=Integer.parseInt(map.get("type_id"));

        if(!userRelationService.isProductAndMerchant(type_id,type)){
            return ResponseUtil.fail("信息不存在");
        }

        //验证用户是否关注过
        UserRelation paramUserRelation=new UserRelation();
        paramUserRelation.setUid(uid);
        paramUserRelation.setType(type);
        paramUserRelation.setTypeId(type_id);
        UserRelation res_UserRelation=userRelationService.queryById(paramUserRelation);
        Map<String,Object> data=new HashMap<String,Object>();

        if(res_UserRelation!=null){
            data.put("is_Relation",1);

        }else{
            data.put("is_Relation",0);
            return  ResponseUtil.ok();
        }

        return  ResponseUtil.ok(data);
    }

    /**
     * 添加关注/收藏
     */

    @PostMapping("/create")
    public Object create(@RequestHeader Map<String,String> Hmap, @RequestBody Map<String,String> map){
        if(!Hmap.containsKey("uid")){
            return ResponseUtil.fail("uid 参数错误");
        }

        if(!map.containsKey("type")||!map.containsKey("type_id")){
            return ResponseUtil.fail("参数错误");
        }
        Integer uid =Integer.parseInt(Hmap.get("uid"));
        Integer type=Integer.parseInt(map.get("type"));
        Integer type_id=Integer.parseInt(map.get("type_id"));

        if(!userRelationService.isProductAndMerchant(type_id,type)){
            return ResponseUtil.fail("信息不存在");
        }

        //验证用户是否关注过
        UserRelation paramUserRelation=new UserRelation();
        paramUserRelation.setUid(uid);
        paramUserRelation.setType(type);
        paramUserRelation.setTypeId(type_id);
        UserRelation res_UserRelation=userRelationService.queryById(paramUserRelation);
        if(res_UserRelation!=null){
            return  ResponseUtil.fail("您已经收藏过了");
        }else{
            paramUserRelation.setCreateTime(new Date());
            userRelationService.insert(paramUserRelation);
            return  ResponseUtil.ok();
        }

    }

    /***
     * 取消关注
     */

    @PostMapping("delete")
    public Object delete(@RequestHeader Map<String,String> Hmap,@RequestBody Map<String,String> map){

        if(!Hmap.containsKey("uid")){
            return ResponseUtil.fail("uid 参数错误");
        }
        if(!map.containsKey("type")||!map.containsKey("type_id")){
            return ResponseUtil.fail("参数错误");
        }
        Integer uid =Integer.parseInt(Hmap.get("uid"));
        Integer type=Integer.parseInt(map.get("type"));
        Integer type_id=Integer.parseInt(map.get("type_id"));

        //验证信息是否存在
        UserRelation paramUserRelation=new UserRelation();
        paramUserRelation.setUid(uid);
        paramUserRelation.setType(type);
        paramUserRelation.setTypeId(type_id);
        UserRelation res_UserRelation=userRelationService.queryById(paramUserRelation);
        if(res_UserRelation==null){
            return ResponseUtil.fail("信息不存在");
        }else {
            userRelationService.deleteById(paramUserRelation);
            return ResponseUtil.ok();
        }


    }


}
