package com.starry.admin.modules.weichat.controller;

import com.starry.admin.common.aspect.ClerkUserLogin;
import com.starry.admin.common.aspect.CustomUserLogin;
import com.starry.admin.common.conf.ThreadLocalRequestDetail;
import com.starry.admin.modules.clerk.module.entity.PlayClerkArticleInfoEntity;
import com.starry.admin.modules.clerk.module.entity.PlayClerkUserInfoEntity;
import com.starry.admin.modules.clerk.module.entity.PlayCustomArticleInfoEntity;
import com.starry.admin.modules.clerk.module.vo.PlayClerkArticleQueryVo;
import com.starry.admin.modules.clerk.service.IPlayClerkUserInfoService;
import com.starry.admin.modules.clerk.service.impl.PlayClerkArticleInfoServiceImpl;
import com.starry.admin.modules.clerk.service.impl.PlayCustomArticleInfoServiceImpl;
import com.starry.admin.modules.weichat.entity.article.PlayClerkAddArticleVo;
import com.starry.admin.modules.weichat.entity.article.PlayClerkArticleCustomQueryVo;
import com.starry.admin.modules.weichat.entity.article.PlayClerkArticleFollowCustomFollowStateEditStateVo;
import com.starry.admin.modules.weichat.entity.article.PlayClerkArticleFollowCustomGreedStateEditStateVo;
import com.starry.admin.modules.weichat.service.WxCustomUserService;
import com.starry.common.result.R;
import com.starry.common.utils.ConvertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 动态接口
 *
 * @author admin
 * @since 2024/5/20 下午11:19
 **/
@Slf4j
@RestController
@RequestMapping("/wx/article")
public class WxArticleController {


    @Resource
    private PlayClerkArticleInfoServiceImpl playClerkArticleInfoService;

    @Resource
    private PlayCustomArticleInfoServiceImpl playCustomArticleInfoService;

    @Resource
    private IPlayClerkUserInfoService playClerkUserInfoService;

    @Resource
    private WxCustomUserService customUserService;


    /**
     * 店员新增动态
     */
    @ClerkUserLogin
    @PostMapping("/clerk/add")
    public R clerkAdd(@Validated @RequestBody PlayClerkAddArticleVo vo) {
        vo.setClerkId(ThreadLocalRequestDetail.getClerkUserInfo().getId());
        playClerkArticleInfoService.create(ConvertUtil.entityToVo(vo, PlayClerkArticleInfoEntity.class));
        return R.ok("成功");
    }


    /**
     * 店员删除
     */
    @ClerkUserLogin
    @GetMapping("/clerk/deleteById")
    public R clerkDeleteById(@RequestParam("id") String id) {
        playClerkArticleInfoService.deletePlayClerkArticleInfoById(id);
        playCustomArticleInfoService.deleteByArticleId(id);
        return R.ok("成功");
    }

    /**
     * 店员分页查询本人动态列表
     */
    @ClerkUserLogin
    @PostMapping("/clerk/listByPage")
    public R clerkListByPage(@Validated @RequestBody PlayClerkArticleQueryVo vo) {
        vo.setClerkId(ThreadLocalRequestDetail.getClerkUserInfo().getId());
        return R.ok(playClerkArticleInfoService.selectByPage(vo,true));
    }


    /**
     * 顾客查询动态列表
     */
    @PostMapping("/custom/listByPage")
    public R customListByPage(@Validated @RequestBody PlayClerkArticleCustomQueryVo vo) {
        vo.setFollowState("0");
        return R.ok(playClerkArticleInfoService.customSelectByPage(vo,customUserService.getLoginUserId()));
    }

    /**
     * 顾客查询已收藏动态列表
     */
    @CustomUserLogin
    @PostMapping("/custom/listFollowByPage")
    public R customListFollowByPage(@Validated @RequestBody PlayClerkArticleCustomQueryVo vo) {
        vo.setFollowState("1");
        return R.ok(playClerkArticleInfoService.customSelectByPage(vo, customUserService.getLoginUserId()));
    }

    /**
     * 分页获取店员动态
     *
     * @param id 店员ID
     * @return 店员动态
     */
    @GetMapping("/custom/queryByClerkId")
    public R queryTrendsById(@RequestParam("id") String id) {
        PlayClerkUserInfoEntity entity = playClerkUserInfoService.selectById(id);
        PlayClerkArticleCustomQueryVo vo = new PlayClerkArticleCustomQueryVo();
        vo.setClerkId(entity.getId());
        return R.ok(playClerkArticleInfoService.customSelectByPage(new PlayClerkArticleCustomQueryVo(), customUserService.getLoginUserId()));
    }


    /**
     * 顾客查询已收藏动态列表
     */
    @CustomUserLogin
    @PostMapping("/custom/updateGreedState")
    public R customUpdateGreedState(@Validated @RequestBody PlayClerkArticleFollowCustomGreedStateEditStateVo vo) {
        PlayClerkArticleInfoEntity articleInfoEntity = playClerkArticleInfoService.selectPlayClerkArticleInfoById(vo.getId());
        PlayCustomArticleInfoEntity entity = playCustomArticleInfoService.selectByArticleId(articleInfoEntity.getId(), ThreadLocalRequestDetail.getCustomUserInfo().getId(), "0");
        if (entity == null) {
            entity = new PlayCustomArticleInfoEntity();
            entity.setArticleId(articleInfoEntity.getId());
            entity.setCustomId(ThreadLocalRequestDetail.getCustomUserInfo().getId());
            entity.setEndorseType("1");
            entity.setClerkId(articleInfoEntity.getClerkId());
        }
        entity.setEndorseTime(LocalDateTime.now());
        entity.setEndorseState(vo.getGreedState());
        playCustomArticleInfoService.saveOrUpdate(entity);
        return R.ok("成功");
    }

    @CustomUserLogin
    @PostMapping("/custom/updateFollowState")
    public R customUpdateFollowState(@Validated @RequestBody PlayClerkArticleFollowCustomFollowStateEditStateVo vo) {
        PlayClerkArticleInfoEntity articleInfoEntity = playClerkArticleInfoService.selectPlayClerkArticleInfoById(vo.getId());
        PlayCustomArticleInfoEntity entity = playCustomArticleInfoService.selectByArticleId(articleInfoEntity.getId(), ThreadLocalRequestDetail.getCustomUserInfo().getId(), "1");
        if (entity == null) {
            entity = new PlayCustomArticleInfoEntity();
            entity.setArticleId(articleInfoEntity.getId());
            entity.setCustomId(ThreadLocalRequestDetail.getCustomUserInfo().getId());
            entity.setEndorseType("0");
            entity.setClerkId(articleInfoEntity.getClerkId());
        }
        entity.setEndorseTime(LocalDateTime.now());
        entity.setEndorseState(vo.getFollowState());
        playCustomArticleInfoService.saveOrUpdate(entity);
        return R.ok("成功");
    }


}
