package com.hzs.sale.product.controller.api;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.enums.ELanguages;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.sale.product.BdAreaClassifySpecialArea;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.sale.product.controller.manage.params.AreaClassifyParam;
import com.hzs.sale.product.service.IBdAreaClassifyService;
import com.hzs.sale.product.service.IBdAreaClassifySpecialAreaService;
import com.hzs.sale.product.vo.AreaClassifyVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Description: 商品分类列表
 * @Author: yuhui
 * @Time: 2022/9/5 10:57
 * @Classname: BdAreaClassifyController
 * @PackageName: com.hzs.sale.product.controller.manage
 */
@Slf4j
@RestController
@RequestMapping("/api/area-classify")
public class ApiAreaClassifyController extends BaseController {

    @Autowired
    private IBdAreaClassifyService areaClassifyService;
    @Autowired
    private IBdAreaClassifySpecialAreaService areaClassifySpecialAreaService;

    /**
     * 查询分类列表
     *
     * @return
     */
    @Log(module = EOperationModule.AREA_CLASSIFY_LIST, method = EOperationMethod.SELECT)
    @GetMapping("list")
    public AjaxResult list(AreaClassifyParam areaClassifyParam) {
        areaClassifyParam.setEnableState(EYesNo.YES.getIntValue());
        Integer pkCountry;
        if (areaClassifyParam.getPkCountry() != null) {
            pkCountry = areaClassifyParam.getPkCountry();
        } else {
            pkCountry = SecurityUtils.getPkCountry();
        }

        // 临时处理分类名称国际化
        this.handleLanguage(areaClassifyParam);

        areaClassifyParam.setPkCountry(pkCountry);
        List<AreaClassifyVo> list = areaClassifyService.selectByAreaClassifyList(areaClassifyParam);
        for (AreaClassifyVo areaClassifyVo : list) {
            QueryWrapper<BdAreaClassifySpecialArea> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("PK_AREA_CLASSIFY", areaClassifyVo.getPkId());
            List<Integer> specialValue = new ArrayList<>();
            List<BdAreaClassifySpecialArea> specialList = areaClassifySpecialAreaService.list(queryWrapper);
            for (BdAreaClassifySpecialArea cps : specialList) {
                specialValue.add(cps.getSpecialArea());
            }
            areaClassifyVo.setSpecialArea(specialValue);
        }
        return AjaxResult.success(list);
    }


    /**
     * 查询第一层分类
     */
    @GetMapping("get-first-classify")
    public AjaxResult getFirstClassify() {
        AreaClassifyParam areaClassifyParam = new AreaClassifyParam();
        areaClassifyParam.setHierarchy(0);

        // 临时处理分类名称国际化
        this.handleLanguage(areaClassifyParam);

        List<AreaClassifyVo> list = areaClassifyService.selectByAreaClassifyList(areaClassifyParam);
        return AjaxResult.success(list);
    }

    /**
     * 查询
     *
     * @param areaClassifyParam
     * @return
     */
    @GetMapping("get-hierarchy-list")
    public AjaxResult getLevelList(AreaClassifyParam areaClassifyParam) {
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        treeNodeConfig.setDeep(4);

        // 临时处理分类名称国际化
        this.handleLanguage(areaClassifyParam);

        List<AreaClassifyVo> list = areaClassifyService.selectByAreaClassifyList(areaClassifyParam);
        List<Tree<String>> build = TreeUtil.build(list, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    tree.setId(treeNode.getPkId().toString());
                    tree.setParentId(treeNode.getPkParent().toString());
                    tree.setName(treeNode.getClassifyName());
                    tree.put("value", treeNode.getPkId().toString());
                    tree.put("label", treeNode.getClassifyName());
                });
        return AjaxResult.success(build);
    }

    /**
     * 处理国际化
     *
     * @param areaClassifyParam
     */
    private void handleLanguage(AreaClassifyParam areaClassifyParam) {
        String languageTag = LocaleContextHolder.getLocale().toLanguageTag();
        if (StringUtils.isNotEmpty(languageTag)) {
            ELanguages language = ELanguages.getLanguagesByLocal(languageTag);
            if (null != language) {
                areaClassifyParam.setLanguageColumn(language.getColumn());
            }
        }
    }

}
