package com.hzya.frame.nc.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
//import com.hzya.frame.demo.bean.Test;
//import com.hzya.frame.demo.mapper.TestMapper;
import com.hzya.frame.nc.entity.NcInterface;
import com.hzya.frame.nc.service.INcInterfaceService;
import com.hzya.frame.sys.user.service.IUserService;
import com.hzya.frame.web.action.ApplicationContextUtil;
import com.hzya.frame.web.entity.JsonResultEntity;
import com.hzya.frame.web.exception.BaseSystemException;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @Content
 * @Author 👻👻👻👻👻👻👻👻👻👻 gjh
 * @Date 2023-06-13 14:29
 * @Version 1.0
 */
@Service(value = "ncInterfaceService")
public class NcInterfaceServiceImpl implements INcInterfaceService {
    public final static Logger log = LoggerFactory.getLogger(NcInterfaceServiceImpl.class);
//    @Resource
//    private TestMapper testMapper;
    @Resource
    private IUserService userService;
    @Override
    public void queryNccBasicArchives(JSONObject req) {
        JSONObject jsonStrObj = req.getJSONObject("jsonStr");
        //基础档案类型
        String type = jsonStrObj.getString("type");
        //查询的参数
        JSONObject queryParameters = jsonStrObj.getJSONObject("queryParameters");
        switch (type) {
            case "1":
                //供应商视图
                String view_name = "v_hzya_nc_supplier";
                //验证参数是否存在视图内
                checkingParametersKeyEffective(view_name, queryParameters);
                break;
            case "2":
                break;
            case "3":
                break;
            default:
                throw new BaseSystemException("未匹配到档案类型！请确认档案类型 1、客户2、供应商3、人员、4项目");
        }


    }

    /****
     * 根据视图名称，验证查询前参数是否有效
     * @content:
     * @author 👻👻👻👻👻👻👻👻 gjh
     * @date 2023-08-08 16:20
     * @param
     * @return void
     **/
    private void checkingParametersKeyEffective(String view_name, JSONObject queryParameters) {
        //查询视图所有列
        List<String> viewFields = new ArrayList<>();
        StringBuffer buffer = new StringBuffer();

        for (String key : queryParameters.keySet()) {
            boolean flag = viewFields.contains(key);
            //不包含key的时候，说明查询参数在视图内不存在，需要抛出去异常
            if (!flag) {
                buffer.append("参数code：" + key + "在视图内不存在！请检查后在传递查询。\n");
            }
        }
        if (buffer.length() > 1) {
            throw new BaseSystemException(buffer.toString());
        }
    }


//    @Override
//    public JsonResultEntity updateUser(JSONObject jsonObject) {
//
//            NcInterface s2 = JSON.parseObject(jsonObject.getString("jsonStr"),NcInterface.class);
//            //System.out.println(JSON.toJSONString(s2));
//        userService.updateEntity(jsonObject);
////        int as = 1/0;
//            JsonResultEntity jsonResultEntity = new JsonResultEntity();
//
//
//            jsonResultEntity.setFlag(true);
////        throw new RuntimeException("系统异常");
//            return jsonResultEntity;
//    }

    @Override
//    @DS("dataSource1")
    public int updateUser1(JSONObject jsonObject) {
//        List<WhereCriterion> whereList = new ArrayList<>();
//        WhereCriterion loginCondition = new WhereCriterion("", "login_name", ConditionType.等于.getValue(), "admin", "", ConnectType.AND);
//        whereList.add(loginCondition);
//        List<ModifyCriterion> modifyExamples = new ArrayList<>();
//        ModifyCriterion modifyCriterion = new ModifyCriterion();
//        modifyCriterion.setFiledName("view_name");
//        modifyCriterion.setFiledValue("超级管理员1");
//        modifyCriterion.setStringValue(true);
//        modifyExamples.add(modifyCriterion);

        SqlSessionFactory s = ApplicationContextUtil.getBeanByClass(SqlSessionFactory.class);
        if (s == null) {
            s = (SqlSessionFactory) ApplicationContextUtil.getBeanByName("mySqlSessionFactory");
        }

        Integer result = s.openSession().update("com.hzya.frame.nc.entity.NcInterface.test_update_user");
//        Integer result = baseDaoUtilImpl.update("sys_user",modifyExamples,whereList);
        log.info("ya251修改人员结果：" + result);
        return result;
    }

    @Override
//    @DS("dataSource2")
    public int updateUser2(JSONObject jsonObject) {
//        List<WhereCriterion> whereList = new ArrayList<>();
//        WhereCriterion loginCondition = new WhereCriterion("", "login_name", ConditionType.等于.getValue(), "admin", "", ConnectType.AND);
//        whereList.add(loginCondition);
//        List<ModifyCriterion> modifyExamples = new ArrayList<>();
//        ModifyCriterion modifyCriterion = new ModifyCriterion();
//        modifyCriterion.setFiledName("view_name");
//        modifyCriterion.setFiledValue("超级管理员3");
//        modifyCriterion.setStringValue(true);
//        modifyExamples.add(modifyCriterion);
//        DataSourceUtil.getDefaultSqlSession()
        SqlSessionFactory s = ApplicationContextUtil.getBeanByClass(SqlSessionFactory.class);
        Integer result = s.openSession().update("test_update_user");
//        Integer result = baseDaoUtilImpl.update("sys_user",modifyExamples,whereList);
        log.info("ya237修改人员结果：" + result);
        return result;
    }

}
