package com.mortals.xhx.busiz.applets.web;


import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.common.Rest;
import com.mortals.framework.service.IAuthTokenService;
import com.mortals.framework.service.IUser;
import com.mortals.xhx.base.system.user.model.UserEntity;
import com.mortals.xhx.base.system.user.model.UserQuery;
import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.busiz.applets.req.FavoriteReq;
import com.mortals.xhx.busiz.applets.req.LoginReq;
import com.mortals.xhx.busiz.applets.req.UserInfoReq;
import com.mortals.xhx.busiz.applets.rsp.FavoriteTypeListInfo;
import com.mortals.xhx.module.bussinesscard.model.BussinesscardEntity;
import com.mortals.xhx.module.bussinesscard.model.BussinesscardQuery;
import com.mortals.xhx.module.bussinesscard.service.BussinesscardService;
import com.mortals.xhx.module.favorites.model.*;
import com.mortals.xhx.module.favorites.service.FavoritesBusinesscardService;
import com.mortals.xhx.module.favorites.service.FavoritesNewsService;
import com.mortals.xhx.module.favorites.service.FavoritesProductService;
import com.mortals.xhx.module.favorites.web.FavoritesBusinesscardController;
import com.mortals.xhx.module.news.model.NewsEntity;
import com.mortals.xhx.module.news.model.NewsQuery;
import com.mortals.xhx.module.news.service.NewsService;
import com.mortals.xhx.module.product.model.ProductEntity;
import com.mortals.xhx.module.product.model.ProductQuery;
import com.mortals.xhx.module.product.service.ProductService;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.model.StaffQuery;
import com.mortals.xhx.module.staff.service.StaffService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @author ZYW
 * @date 2023-10-30 9:39
 */
@RestController
@Slf4j
@RequestMapping("/api/v1/favorite")
public class FavoriteApiController extends AbstractBaseController<FavoriteReq>{

    @Autowired
    private FavoritesProductService favoritesProductService;
    @Autowired
    private FavoritesNewsService favoritesNewsService;
    @Autowired
    private FavoritesBusinesscardService favoritesBusinesscardService;
    @Autowired
    private IAuthTokenService authTokenService;
    @Autowired
    private UserService userService;
    @Autowired
    private ProductService productService;
    @Autowired
    private NewsService newsService;
    @Autowired
    private BussinesscardService bussinesscardService;



    @RequestMapping("favorite")
    public Rest<Object> favorites(@RequestBody FavoriteReq favoriteReq) throws Exception {
        String busiDesc = "收藏产品/新闻/名片";
        Rest<Object> rest = Rest.ok();
        try {
            IUser iUser = authTokenService.getLoginUser(request);
            if(ObjectUtils.isEmpty(iUser)){
                rest = Rest.fail("获取个人信息失败");
            }else {
                if(!ObjectUtils.isEmpty(favoriteReq.getProductId()) && favoriteReq.getProductId() != 0){
                    FavoritesProductEntity favoritesProductEntity = new FavoritesProductEntity();
                    favoritesProductEntity.setUserId(iUser.getId());
                    favoritesProductEntity.setProductId(favoriteReq.getProductId());
                    favoritesProductEntity.setCreateTime(new Date());
                    favoritesProductEntity.setCreateUserId(iUser.getId());
                    favoritesProductEntity.setFavoriteTime(new Date());

                    favoritesProductService.save(favoritesProductEntity);

                    rest.setMsg("收藏产品成功");
                }
                if(!ObjectUtils.isEmpty(favoriteReq.getNewsId()) && favoriteReq.getNewsId() != 0){
                    FavoritesNewsEntity favoritesNewsEntity = new FavoritesNewsEntity();
                    favoritesNewsEntity.setUserId(iUser.getId());
                    favoritesNewsEntity.setNewsId(favoriteReq.getNewsId());
                    favoritesNewsEntity.setCreateTime(new Date());
                    favoritesNewsEntity.setCreateUserId(iUser.getId());
                    favoritesNewsEntity.setFavoriteTime(new Date());

                    favoritesNewsService.save(favoritesNewsEntity);

                    rest.setMsg("收藏新闻成功");
                }
                if(!ObjectUtils.isEmpty(favoriteReq.getStaffId()) && favoriteReq.getStaffId() != 0){
                    FavoritesBusinesscardEntity favoritesBusinesscardEntity = new FavoritesBusinesscardEntity();
                    favoritesBusinesscardEntity.setUserId(iUser.getId());
                    favoritesBusinesscardEntity.setStaffId(favoriteReq.getStaffId());
                    favoritesBusinesscardEntity.setCreateTime(new Date());
                    favoritesBusinesscardEntity.setCreateUserId(iUser.getId());
                    favoritesBusinesscardEntity.setFavoriteTime(new Date());

                    favoritesBusinesscardService.save(favoritesBusinesscardEntity);

                    rest.setMsg("收藏名片成功");
                }
            }

        }catch (Exception e) {
            log.error(busiDesc, e);
            rest = Rest.fail(super.convertException(e));
        }

        return rest;
    }


    /**
     * 查询收藏分类
     * @return
     * @throws Exception
     */
    @RequestMapping("myFavoriteType")
    public Rest<Object> myFavoriteType() {
        String busiDesc = "我的收藏";
        Rest<Object> rest = Rest.ok();
        try {
            List<FavoriteTypeListInfo> list = new ArrayList<>();
            list.add(new FavoriteTypeListInfo(1,"产品收藏"));
            list.add(new FavoriteTypeListInfo(2,"新闻收藏"));
            list.add(new FavoriteTypeListInfo(3,"名片收藏"));
            rest.setData(list);
        }catch (Exception e) {
            log.error(busiDesc, e);
            rest = Rest.fail(super.convertException(e));
        }

        return rest;
    }


    /**
     * 查询我的收藏
     * @return
     * @throws Exception
     */
    @RequestMapping("myFavorite")
    public Rest<Object> myFavorites(@RequestBody FavoriteReq favoriteReq) {
        String busiDesc = "我的收藏";
        Rest<Object> rest = Rest.ok();
        try {
            IUser iUser = authTokenService.getLoginUser(request);
            long userId = iUser.getId();
            if(ObjectUtils.isEmpty(iUser)){
                rest = Rest.fail("获取个人信息失败");
            }else {
                if(!ObjectUtils.isEmpty(favoriteReq) && favoriteReq.getTypeId() != 0){
                    switch (favoriteReq.getTypeId()){
                        case 1:   //产品收藏
                            List<FavoritesProductEntity> productList = favoritesProductService.find(new FavoritesProductQuery().id(userId));
                            if(!ObjectUtils.isEmpty(productList)){
                                List<ProductEntity> list = new ArrayList<>();
                                for (int i = 0; i < productList.size(); i++) {
                                    long id = productList.get(i).getProductId();
                                    ProductEntity productEntity = productService.selectOne(new ProductQuery().id(id));
                                    if(!ObjectUtils.isEmpty(productEntity)){
                                        list.add(productEntity);
                                    }
                                }

                                rest.setData(list);
                            }else {
                                rest.setMsg("未收藏产品");
                            }
                            break;
                        case 2:   //新闻收藏
                            List<FavoritesNewsEntity> newsList = favoritesNewsService.find(new FavoritesNewsQuery().id(userId));
                            if(!ObjectUtils.isEmpty(newsList)){
                                List<NewsEntity> list = new ArrayList<>();
                                for (int i = 0; i < newsList.size(); i++) {
                                    long id = newsList.get(i).getNewsId();
                                    NewsEntity newsEntity = newsService.selectOne(new NewsQuery().id(id));
                                    if(!ObjectUtils.isEmpty(newsEntity)){
                                        list.add(newsEntity);
                                    }
                                }

                                rest.setData(list);
                            }else {
                                rest.setMsg("未收藏新闻");
                            }
                            break;
                        case 3:   //名片收藏
                            List<FavoritesBusinesscardEntity> cardList = favoritesBusinesscardService.find(new FavoritesBusinesscardQuery().id(userId));
                            if(!ObjectUtils.isEmpty(cardList)){
                                List<BussinesscardEntity> list = new ArrayList<>();
                                for (int i = 0; i < cardList.size(); i++) {
                                    long id = cardList.get(i).getStaffId();
                                    BussinesscardEntity bussinesscardEntity = bussinesscardService.selectOne(new BussinesscardQuery().id(id));
                                    if(!ObjectUtils.isEmpty(bussinesscardEntity)){
                                        list.add(bussinesscardEntity);
                                    }
                                }
                                rest.setData(list);
                            }else {
                                rest.setMsg("未收藏名片");
                            }
                            break;
                        default:
                            rest = Rest.fail("传入类型id错误");
                            break;
                    }
                }else {
                    rest = Rest.fail("必须传入类型id");
                }


            }

        }catch (Exception e) {
            log.error(busiDesc, e);
            rest = Rest.fail(super.convertException(e));
        }

        return rest;
    }

}
