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

import com.starry.admin.common.aspect.ClerkUserLogin;
import com.starry.admin.common.conf.ThreadLocalRequestDetail;
import com.starry.admin.common.exception.CustomException;
import com.starry.admin.modules.clerk.module.entity.PlayClerkCommodityEntity;
import com.starry.admin.modules.clerk.module.entity.PlayClerkUserInfoEntity;
import com.starry.admin.modules.clerk.service.IPlayClerkCommodityService;
import com.starry.admin.modules.clerk.service.IPlayClerkUserInfoService;
import com.starry.admin.modules.shop.module.entity.PlayCommodityAndLevelInfoEntity;
import com.starry.admin.modules.shop.service.IPlayCommodityAndLevelInfoService;
import com.starry.admin.modules.shop.service.IPlayCommodityInfoService;
import com.starry.admin.modules.weichat.entity.PlayCommodityReturnVo;
import com.starry.common.result.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 店员等级
 *
 * @author admin
 * @since 2024/5/29 上午6:24
 **/
@Api(tags = "微信店员商品接口", description = "微信端店员商品相关接口")
@Slf4j
@RestController
@RequestMapping("/wx/commodity/")
public class WxClerkCommodityController {

    @Resource
    private IPlayCommodityInfoService playCommodityInfoService;

    @Resource
    private IPlayCommodityAndLevelInfoService iPlayCommodityAndLevelInfoService;

    @Resource
    private IPlayClerkUserInfoService clerkUserInfoService;

    @Resource
    IPlayClerkCommodityService playClerkCommodityService;

    @ApiOperation(value = "根据等级查询商品", notes = "根据等级ID查询对应等级的所有商品")
    @ApiImplicitParam(name = "id", value = "等级ID", dataType = "String", paramType = "query")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = PlayCommodityReturnVo.class, responseContainer = "List")})
    @GetMapping("/custom/queryClerkAllCommodityByLevel")
    public R queryClerkAllCommodityByLevel(@RequestParam(value = "id", required = false) String levelId) {
        List<PlayCommodityAndLevelInfoEntity> levelInfoEntities = iPlayCommodityAndLevelInfoService.selectAll();
        List<PlayCommodityReturnVo> tree = playCommodityInfoService.selectTree();
        if (levelId == null || levelId.isEmpty()) {
            return R.ok(tree);
        }
        tree = formatPlayCommodityReturnVoTree(tree, levelInfoEntities, levelId);
        tree = formatPlayCommodityReturnVoTree(tree, null);
        return R.ok(tree);
    }

    /**
     * 顾客查询当前店员所有服务项目
     *
     * @return 店员所有服务项目
     */
    @ApiOperation(value = "查询店员商品", notes = "根据店员ID查询该店员提供的所有商品服务")
    @ApiImplicitParam(name = "id", value = "店员ID", required = true, dataType = "String", paramType = "query")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = PlayCommodityReturnVo.class, responseContainer = "List"),
            @ApiResponse(code = 500, message = "请求参数异常,id不能为空")})
    @GetMapping("/custom/queryClerkAllCommodity")
    public R customQueryClerkAllCommodity(@RequestParam("id") String clerkId) {
        if (clerkId == null || clerkId.isEmpty()) {
            throw new CustomException("请求参数异常,id不能为空");
        }
        PlayClerkUserInfoEntity clerkUserInfo = clerkUserInfoService.selectById(clerkId);
        Map<String, List<PlayClerkCommodityEntity>> clerkCommodityEntities = playClerkCommodityService
                .selectCommodityTypeByUser(clerkId, "1").stream()
                .collect(Collectors.groupingBy(PlayClerkCommodityEntity::getCommodityId));
        List<PlayCommodityAndLevelInfoEntity> levelInfoEntities = iPlayCommodityAndLevelInfoService.selectAll();
        List<PlayCommodityReturnVo> tree = playCommodityInfoService.selectTree();
        tree = formatPlayCommodityReturnVoTree(tree, levelInfoEntities, clerkUserInfo.getLevelId());
        tree = formatPlayCommodityReturnVoTree(tree, clerkCommodityEntities);
        return R.ok(tree);
    }

    /**
     * 店员查询自己的服务项目
     *
     * @return 店员所有服务项目
     */
    @ApiOperation(value = "店员查询自己的商品", notes = "当前登录店员查询自己提供的所有商品服务")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功", response = PlayCommodityReturnVo.class, responseContainer = "List")})
    @ClerkUserLogin
    @GetMapping("/clerk/queryAllCommodity")
    public R clerkQueryAllCommodity() {
        String levelId = ThreadLocalRequestDetail.getClerkUserInfo().getLevelId();
        List<PlayCommodityAndLevelInfoEntity> levelInfoEntities = iPlayCommodityAndLevelInfoService.selectAll();
        Map<String, List<PlayClerkCommodityEntity>> clerkCommodityEntities = playClerkCommodityService
                .selectCommodityTypeByUser(ThreadLocalRequestDetail.getClerkUserInfo().getId(), "1").stream()
                .collect(Collectors.groupingBy(PlayClerkCommodityEntity::getCommodityId));

        List<PlayCommodityReturnVo> tree = playCommodityInfoService.selectTree();
        tree = formatPlayCommodityReturnVoTree(tree, levelInfoEntities, levelId);
        tree = formatPlayCommodityReturnVoTree(tree, clerkCommodityEntities);
        return R.ok(tree);
    }

    public List<PlayCommodityReturnVo> formatPlayCommodityReturnVoTree(List<PlayCommodityReturnVo> tree,
            List<PlayCommodityAndLevelInfoEntity> levelInfoEntities, String levelId) {
        Iterator<PlayCommodityReturnVo> it = tree.iterator();
        while (it.hasNext()) {
            PlayCommodityReturnVo item = it.next();
            // 查找当前服务项目对应的价格
            for (PlayCommodityAndLevelInfoEntity levelInfoEntity : levelInfoEntities) {
                if (item.getId().equals(levelInfoEntity.getCommodityId())
                        && levelId.equals(levelInfoEntity.getLevelId())) {
                    item.setPrice(levelInfoEntity.getPrice());
                }
            }
            // 如果未设置价格,删除元素
            if (!"00".equals(item.getPId()) && item.getPrice() == null) {
                it.remove();
            }
            formatPlayCommodityReturnVoTree(item.getChild(), levelInfoEntities, levelId);
        }
        return tree;
    }

    public List<PlayCommodityReturnVo> formatPlayCommodityReturnVoTree(List<PlayCommodityReturnVo> tree,
            Map<String, List<PlayClerkCommodityEntity>> clerkCommodityEntities) {
        Iterator<PlayCommodityReturnVo> it = tree.iterator();
        while (it.hasNext()) {
            PlayCommodityReturnVo item = it.next();
            if ("00".equals(item.getPId()) && item.getChild().isEmpty()) {
                it.remove();
            } else if (clerkCommodityEntities != null && "00".equals(item.getPId())
                    && !clerkCommodityEntities.containsKey(item.getId())) {
                it.remove();
            }
            formatPlayCommodityReturnVoTree(item.getChild(), clerkCommodityEntities);
        }
        return tree;
    }

}
