
package com.tmerclub.cloud.user.controller.platform;

import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.user.constant.TagTypeEnum;
import com.tmerclub.cloud.user.dto.UserTagDTO;
import com.tmerclub.cloud.user.model.UserTag;
import com.tmerclub.cloud.user.service.UserTagService;
import com.tmerclub.cloud.user.vo.UserTagVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 客户标签
 *
 * @author cl
 * @date 2020-05-17 16:16:53
 */
@RestController("platformUserTagController")
@RequestMapping("/p/userTag")
@Tag(name = "platform-客户标签")
public class UserTagController {

    @Autowired
    private UserTagService userTagService;


    @GetMapping("/page")
    @Operation(summary = "获取客户标签列表", description = "分页获取客户标签列表")
    public ServerResponseEntity<PageVO<UserTagVO>> page(@Valid PageDTO pageDTO) {
        PageVO<UserTagVO> userTagPage = userTagService.page(pageDTO);
        return ServerResponseEntity.success(userTagPage);
    }

    @GetMapping
    @Operation(summary = "获取客户标签", description = "根据userTagId获取客户标签")
    public ServerResponseEntity<UserTagVO> getByUserTagId(@RequestParam Long userTagId) {
        UserTagVO userTagVO = BeanUtil.map(userTagService.getByUserTagId(userTagId), UserTagVO.class);
        return ServerResponseEntity.success(userTagVO);
    }

    @PostMapping
    @Operation(summary = "保存客户标签", description = "保存客户标签")
    public ServerResponseEntity<Void> save(@RequestBody @Validated(UserTagDTO.AddUserTag.class) UserTagDTO userTagDTO) {
        UserTag query = new UserTag();
        // 条件标签和手动标签的标签名称都不可以相同
        query.setTagType(TagTypeEnum.CONDITION.value());
        int count = userTagService.count(query);
        if (Objects.equals(userTagDTO.getTagType(), TagTypeEnum.CONDITION.value()) && count + 1 > Constant.TAG_LIMIT_NUM) {
            // 数量超过上限
            throw new LuckException("数量超过上限");
        }
        query.setTagName(userTagDTO.getTagName());
        query.setTagType(null);
        int nameCount = userTagService.count(query);
        if (nameCount > 0) {
            // 标签名称已存在！
            throw new LuckException("标签名称已存在！");
        }
        userTagService.addUserTag(userTagDTO);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新客户标签", description = "更新客户标签")
    public ServerResponseEntity<Void> update(@RequestBody @Validated(UserTagDTO.UpdateUserTag.class) UserTagDTO userTagDTO) {

        UserTag query = new UserTag();
//        query.setTagType(TagTypeEnum.CONDITION.value())
        query.setTagName(userTagDTO.getTagName());
        query.setUserTagId(userTagDTO.getUserTagId());
        int nameCount = userTagService.count(query);
        if (nameCount > 0) {
            // 标签名称已存在！
            throw new LuckException("标签名称已存在！");
        }
        userTagService.updateUserTag(userTagDTO);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除客户标签", description = "根据客户标签id删除客户标签")
    public ServerResponseEntity<Void> delete(@RequestParam Long userTagId) {
        userTagService.deleteById(userTagId);
        return ServerResponseEntity.success();
    }

    @GetMapping("/refresh")
    @Operation(summary = "刷新客户标签统计数据", description = "通过id刷新客户标签统计数据")
    public ServerResponseEntity<UserTagVO> refreshUserTag(@RequestParam Long userTagId) {
        return ServerResponseEntity.success(userTagService.refreshConditionTag(userTagId));
    }

    @Operation(summary = "批量刷新客户标签")
    @Parameter(name = "tagIds", description = "标签id集合（不传默认刷新全部）")
    @GetMapping("/batchRefreshTag")
    public ServerResponseEntity<Void> batchRefreshTag(@RequestParam(required = false) List<Long> tagIds) {
        // 查询所有条件标签
        if (CollectionUtils.isEmpty(tagIds)) {
            UserTag userTag = new UserTag();
            userTag.setTagType(TagTypeEnum.CONDITION.value());
            List<UserTagVO> userTags = userTagService.list(userTag);
            if (CollectionUtils.isEmpty(userTags)) {
                return ServerResponseEntity.success();
            }
            tagIds = userTags.stream().map(UserTagVO::getUserTagId).collect(Collectors.toList());
        }
        userTagService.batchRefreshConditionTag(tagIds);
        return ServerResponseEntity.success();
    }

    @GetMapping("/getTagList")
    @Operation(summary = "可以添加的标签列表", description = "可以添加的标签列表")
    public ServerResponseEntity<List<UserTagVO>> getTagList() {
        return ServerResponseEntity.success(userTagService.list(new UserTag()));
    }


    @GetMapping("/tagPage")
    @Operation(summary = "获取客户标签列表", description = "分页获取客户标签列表")
    public ServerResponseEntity<PageVO<UserTagVO>> getTagPage(@Valid PageDTO pageDTO, UserTagDTO userTagDTO) {
        PageVO<UserTagVO> resPage = userTagService.getPage(pageDTO, userTagDTO);
        return ServerResponseEntity.success(resPage);
    }

}
