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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.time.LocalDateTime;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 动态接口
 *
 * @author admin
 * @since 2024/5/20 下午11:19
 **/
@Api(tags = "微信文章动态接口", description = "微信端文章动态相关接口")
@Slf4j
@RestController
@RequestMapping("/wx/article")
public class WxArticleController {

    @Resource
    private PlayClerkArticleInfoServiceImpl playClerkArticleInfoService;

    @Resource
    private PlayCustomArticleInfoServiceImpl playCustomArticleInfoService;

    @Resource
    private IPlayClerkUserInfoService playClerkUserInfoService;

    @Resource
    private WxCustomUserService customUserService;

    /**
     * 店员新增动态
     */
    @ApiOperation(value = "店员新增动态", notes = "店员发布新的动态文章")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @ClerkUserLogin
    @PostMapping("/clerk/add")
    public R clerkAdd(@ApiParam(value = "动态信息", required = true) @Validated @RequestBody PlayClerkAddArticleVo vo) {
        vo.setClerkId(ThreadLocalRequestDetail.getClerkUserInfo().getId());
        playClerkArticleInfoService.create(ConvertUtil.entityToVo(vo, PlayClerkArticleInfoEntity.class));
        return R.ok("成功");
    }

    /**
     * 店员删除
     */
    @ApiOperation(value = "店员删除动态", notes = "店员删除自己发布的动态")
    @ApiImplicitParam(name = "id", value = "动态ID", required = true, dataType = "String", paramType = "query")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @ClerkUserLogin
    @GetMapping("/clerk/deleteById")
    public R clerkDeleteById(@RequestParam("id") String id) {
        playClerkArticleInfoService.deletePlayClerkArticleInfoById(id);
        playCustomArticleInfoService.deleteByArticleId(id);
        return R.ok("成功");
    }

    /**
     * 店员分页查询本人动态列表
     */
    @ApiOperation(value = "店员查询动态", notes = "店员分页查询自己的动态列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = PlayClerkArticleInfoEntity.class, responseContainer = "Page")})
    @ClerkUserLogin
    @PostMapping("/clerk/listByPage")
    public R clerkListByPage(
            @ApiParam(value = "查询条件", required = true) @Validated @RequestBody PlayClerkArticleQueryVo vo) {
        vo.setClerkId(ThreadLocalRequestDetail.getClerkUserInfo().getId());
        return R.ok(playClerkArticleInfoService.selectByPage(vo, true));
    }

    /**
     * 顾客查询动态列表
     */
    @ApiOperation(value = "顾客查询动态列表", notes = "顾客分页查询所有动态列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = PlayClerkArticleInfoEntity.class, responseContainer = "Page")})
    @PostMapping("/custom/listByPage")
    public R customListByPage(
            @ApiParam(value = "查询条件", required = true) @Validated @RequestBody PlayClerkArticleCustomQueryVo vo) {
        vo.setFollowState("0");
        return R.ok(playClerkArticleInfoService.customSelectByPage(vo, customUserService.getLoginUserId()));
    }

    /**
     * 顾客查询已收藏动态列表
     */
    @ApiOperation(value = "查询收藏动态", notes = "顾客查询已收藏的动态列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = PlayClerkArticleInfoEntity.class, responseContainer = "Page")})
    @CustomUserLogin
    @PostMapping("/custom/listFollowByPage")
    public R customListFollowByPage(
            @ApiParam(value = "查询条件", required = true) @Validated @RequestBody PlayClerkArticleCustomQueryVo vo) {
        vo.setFollowState("1");
        return R.ok(playClerkArticleInfoService.customSelectByPage(vo, customUserService.getLoginUserId()));
    }

    /**
     * 分页获取店员动态
     *
     * @param id
     *            店员ID
     * @return 店员动态
     */
    @ApiOperation(value = "获取店员动态", notes = "根据店员ID查询该店员的动态")
    @ApiImplicitParam(name = "id", value = "店员ID", required = true, dataType = "String", paramType = "query")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = PlayClerkArticleInfoEntity.class, responseContainer = "Page")})
    @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(vo, customUserService.getLoginUserId()));
    }

    /**
     * 顾客查询已收藏动态列表
     */
    @ApiOperation(value = "更新点赞状态", notes = "顾客更新对动态的点赞状态")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @CustomUserLogin
    @PostMapping("/custom/updateGreedState")
    public R customUpdateGreedState(
            @ApiParam(value = "点赞信息", required = true) @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("成功");
    }

    @ApiOperation(value = "更新收藏状态", notes = "顾客更新对动态的收藏状态")
    @ApiResponses({@ApiResponse(code = 200, message = "操作成功")})
    @CustomUserLogin
    @PostMapping("/custom/updateFollowState")
    public R customUpdateFollowState(
            @ApiParam(value = "收藏信息", required = true) @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("成功");
    }

}
