package com.tofly.feesapi.common;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.tofly.feesapi.common.core.ResultRespone;

import com.tofly.feesapi.common.mapper.CommonMapper;
import com.tofly.feesapi.common.model.CollectionData;
import com.tofly.feesapi.yhgl.service.YhsjqxService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@AllArgsConstructor
@RequestMapping("/EsayController")
@Api(tags = "EsayController")
@Slf4j
public class EsayCommController extends BaseController {

    private CommonMapper commonMapper;

    @Autowired
    private PermissionVerifyService permissionVerifyService;
    @Autowired
    private YhsjqxService yhsjqxService;

    @ApiOperation(value = "公用码表查询")
    @GetMapping("/getCode")
    public ResultRespone getCode(@ApiParam(value = "表名", required = true)
                                 @RequestParam String tableName,
                                 @ApiParam(value = "key值", required = true)
                                 @RequestParam String key,
                                 @ApiParam(value = "value值", required = true)
                                 @RequestParam String value,
                                 @ApiParam(value = "排序关键字")
                                 @RequestParam String orderField,
                                 @ApiParam(value = "排序[ asc ,desc]", allowableValues = "asc,desc")
                                 @RequestParam String orderWay,
                                 @ApiParam(value = "查询对象")
                                 @RequestParam Object strWhere
    ) throws Exception {

        String sql = String.format("select %s as \"" + key + "\",%s as \"" + value + "\" from %s where 1=1", key, value, tableName);
        String str = ObjectUtils.toString(strWhere, "");

        if (!StringUtils.isBlank(str)) {
            JSONObject jsonObj = JSONObject.parseObject(str);
            for (Map.Entry<String, Object> entry : jsonObj.entrySet()) {

                if (entry.getValue() != null && StringUtils.isNotBlank(String.valueOf(entry.getValue()))) {
                    if (entry.getKey().equals("pqfzr")) {
                        if (!permissionVerifyService.VerifyManager()) {
                            sql += String.format(" and %s = '%s' ", entry.getKey(), entry.getValue());
                        }
                    } else {
                        sql += String.format(" and %s = '%s' ", entry.getKey(), entry.getValue());
                    }
                }
            }
        }

        if (!StringUtils.isBlank(orderField)) {
            sql += String.format(" order by %s %s", orderField, orderWay);
        }
        //System.out.println(sql);
        List<HashMap<String, Object>> rs = commonMapper.query(sql);
        CollectionData data = new CollectionData(rs, rs.size());

        // 抄表册权限控制
        if ("bm_cbb".equalsIgnoreCase(tableName) && CollectionUtil.isNotEmpty(rs)) {
            List<String> cbbmList = yhsjqxService.queryByPermission();
            List<HashMap<String, Object>> dataList = new ArrayList<>();
            for (HashMap<String, Object> cbbObj : rs) {
                String cbbbm = (String)cbbObj.get("cbbbm");
                if (cbbmList.contains(cbbbm)) {
                    dataList.add(cbbObj);
                }
            }
            data = new CollectionData(dataList, dataList.size());
        }
        return ResultRespone.success(data);
    }

}
