package com.dk.mdm.service.sys;

import com.dk.common.mapper.BaseMapper;
import com.dk.common.response.ResponseResultUtil;
import com.dk.common.response.ResponseResultVO;
import com.dk.common.service.BaseService;
import com.dk.mdm.mapper.sys.PageItemMapper;
import com.dk.common.model.pojo.system.PageItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @desc   : 页面显示项目
 * @date   : 2022/5/20 9:56
 * @author : 寇珊珊
 */
@Service
@Transactional
public class PageItemService extends BaseService<PageItem> {

    @Override
    public BaseMapper<PageItem> getRepository() {
        return pageItemMapper;
    }

    @Autowired
    private PageItemMapper pageItemMapper;


    /**
     * @desc : 树形结构
     * @date : 2022/5/11 13:18
     * @author : 寇珊珊
     */
    public ResponseResultVO selectTreeByDoc(PageItem pageItem) {
        // 系统单据的树形结构
        List<Map<String, Object>> list = pageItemMapper.selectTreeByDoc(pageItem);
        return ResponseResultUtil.success(list);
    }

    /*
     * @desc   : 根据docCode查询可打印的pageItem
     * @author : 沈博
     * @date   : 2022/6/28 10:50
     * @todo   : 国际化写死了先 2022年6月28日11:40:12

     */
    public ResponseResultVO selectPageItemByDoc(List<Map<String,Object>> maps){
        List<Map<String,Object>> resultMap = new ArrayList<>();
        maps.forEach(it->{
            Map<String, Object> pageItemMap = new HashMap<>();
            Map<String,Object> docNameI18n = (Map<String,Object>)it.get("docNameI18n");
            pageItemMap.put("label", (docNameI18n.get("zh_CN")));
            pageItemMap.put("value",it.get("docCode"));
            /* 设置dataSource */
            List<PageItem> pageItems = this.selectListByCond(new PageItem().setLan("zh_CN").setFlgPrintItem(true).setDocCode(String.valueOf(it.get("docCode"))));
            List<Map<String,Object>> mainList = new ArrayList<>();
            List<Map<String,Object>> detailList = new ArrayList<>();
            List<Map<String,Object>> tableColTempList = new ArrayList<>();
            pageItems.forEach(pageItem->{
                Map<String,Object> otherInfo = pageItem.getItemTags();
                if(pageItem.getGroupType() == 1){
                    mainList.add(new HashMap<String,Object>(){{
                        put("title",pageItem.getItemName());
                        put("key",pageItem.getObjectName());
                        put("value",otherInfo.get("print_def_val"));
                    }});
                }
                if(pageItem.getGroupType() == 2){
                    detailList.add(new HashMap<String,Object>(){{
                        put("title",pageItem.getItemName());
                        put("key",pageItem.getObjectName());
                        put("value",otherInfo.get("print_def_val"));
                    }});
                    if(Boolean.parseBoolean(otherInfo.get("def_show_flag").toString())){
                        tableColTempList.add(new HashMap<String,Object>(){{
                            put("title",pageItem.getItemName());
                            put("key",pageItem.getObjectName());
                            put("width",10);
                            put("fontSize",14);
                            put("align1","center");
                            put("align2","center");
                            put("fontStyle",false);
                            put("bold",false);
                        }});
                    }
                }
            });
            pageItemMap.put("dataSource",new HashMap<String,Object>(){{put("main",mainList);put("detail",detailList);}});
            pageItemMap.put("tableColTemp",tableColTempList);
            resultMap.add(pageItemMap);
        });
        return ResponseResultUtil.success(resultMap);
    }


    /*
     * @desc   : 下划线转驼峰
     * @author : 沈博
     * @date   : 2022/6/28 10:59
     */
    public String x_x2xX(String str) {
        str = str.toLowerCase();
        Pattern compile = Pattern.compile("_[a-z]");
        Matcher matcher = compile.matcher(str);
        StringBuffer sb = new StringBuffer();
        while(matcher.find()) {
            matcher.appendReplacement(sb,  matcher.group(0).toUpperCase().replace("_",""));
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}
