package com.hzya.frame.sysnew.generalData.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.basedao.service.impl.BaseService;
import com.hzya.frame.sysnew.generalData.dao.IGeneralDataDao;
import com.hzya.frame.sysnew.generalData.entity.GeneralDataEntity;
import com.hzya.frame.sysnew.generalData.service.IGeneralDataService;
import com.hzya.frame.sysnew.homeComponent.dao.ISysHomeComponentDao;
import com.hzya.frame.uuid.UUIDLong;
import com.hzya.frame.uuid.UUIDUtils;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service(value = "generalDataServiceImpl")
public class GeneralDataServiceImpl extends BaseService<GeneralDataEntity, String> implements IGeneralDataService {
    private IGeneralDataDao generalDataDao;

    @Autowired
    public void setGeneralDataDao(IGeneralDataDao dao) {
        this.generalDataDao = dao;
        this.dao = dao;
    }

    //新增档案对照
    @Override
    public JsonResultEntity saveEntity(JSONObject json) {
        JSONObject jsonObject = json.getJSONObject("jsonStr");
        if(jsonObject == null ){
            return BaseResult.getFailureMessageEntity("数据为空，请先传递数据");
        }
        if(!checkStr(jsonObject.getString("archivesType"))){
            return BaseResult.getFailureMessageEntity("档案对照类型为空");
        }
        if(!checkStr(jsonObject.getString("leftCode"))){
            return BaseResult.getFailureMessageEntity("左编码为空");
        }
        if(!checkStr(jsonObject.getString("rightCode"))){
            return BaseResult.getFailureMessageEntity("右编码为空");
        }
        if(!checkStr(jsonObject.getString("leftAppCode"))){
            return BaseResult.getFailureMessageEntity("左应用编码为空");
        }
        if(!checkStr(jsonObject.getString("rightAppCode"))){
            return BaseResult.getFailureMessageEntity("右应用编码为空");
        }
        List<String> fzhs = Arrays.asList(new String[]{"1", "2", "6", "73", "D01" , "jobass" , "93" , "96", "22"});
        if(!fzhs.contains(jsonObject.getString("archivesType"))){
            return BaseResult.getFailureMessageEntity("档案类型错误，请联系管理员");
        }
       /* switch (jsonObject.getString("archivesType")){
            case "1"://人员对照
                GeneralDataEntity generalDataEntityPersonnel = jsonObject.toJavaObject(GeneralDataEntity.class);
                generalDataDao.saveGeneralDataByType(generalDataEntityPersonnel);
                break;
            case "2"://部门档案
                GeneralDataEntity generalDataEntityOrg = jsonObject.toJavaObject(GeneralDataEntity.class);
               generalDataDao.saveGeneralDataByType(generalDataEntityOrg);
                break;
            case "6"://存货对照
                GeneralDataEntity generalDataEntityStock = jsonObject.toJavaObject(GeneralDataEntity.class);
                generalDataDao.saveGeneralDataByType(generalDataEntityStock);
                break;
            case "73"://客商辅助核算
                GeneralDataEntity generalDataEntityMerchant = jsonObject.toJavaObject(GeneralDataEntity.class);
                generalDataDao.saveGeneralDataByType(generalDataEntityMerchant);
                break;
            case "D01"://税率
                GeneralDataEntity generalDataEntityTaxrate = jsonObject.toJavaObject(GeneralDataEntity.class);
                generalDataDao.saveGeneralDataByType(generalDataEntityTaxrate);
                break;
            case "jobass"://项目辅助核算
                GeneralDataEntity generalDataEntityProject = jsonObject.toJavaObject(GeneralDataEntity.class);
                generalDataDao.saveGeneralDataByType(generalDataEntityProject);
                break;
            case "93"://银行类别
                GeneralDataEntity generalDataEntityBank = jsonObject.toJavaObject(GeneralDataEntity.class);
                generalDataDao.saveGeneralDataByType(generalDataEntityBank);
                break;
            case "96"://银行账户
                GeneralDataEntity generalDataEntityBankaccount = jsonObject.toJavaObject(GeneralDataEntity.class);
                generalDataDao.saveGeneralDataByType(generalDataEntityBankaccount);
                break;
            case "22"://资产类别
                GeneralDataEntity generalDataEntityAssets = jsonObject.toJavaObject(GeneralDataEntity.class);
                generalDataDao.saveGeneralDataByType(generalDataEntityAssets);
                break;
            default:
                break;
        }*/
        GeneralDataEntity generalDataEntity = jsonObject.toJavaObject(GeneralDataEntity.class);
        generalDataEntity.setId(UUIDUtils.getUUID());
        generalDataEntity.setCreate_user_id("1");
        generalDataEntity.setModify_user_id("1");
        generalDataEntity.setOrg_id("0");
        generalDataEntity.setCompanyId("0");
        try {
            GeneralDataEntity generalDataEntitySave = generalDataDao.saveGeneralDataByType(generalDataEntity);
            return BaseResult.getSuccessMessageEntity("新增成功",generalDataEntitySave);
        }catch (Exception e){
            logger.info("档案对照新增错误:{}",e.getMessage());
            return BaseResult.getFailureMessageEntity("新增失败");
        }
    }

    //查询档案对照
    @Override
    public JsonResultEntity queryEntityPage(JSONObject json) {
        JSONObject jsonObject = json.getJSONObject("jsonStr");
        if(jsonObject == null ){
            return BaseResult.getFailureMessageEntity("数据为空，请先传递数据");
        }
        if(!checkStr(jsonObject.getString("archivesType"))){
            return BaseResult.getFailureMessageEntity("档案对照类型为空");
        }
        /*if(!checkStr(jsonObject.getString("leftCode"))){
            return BaseResult.getFailureMessageEntity("左编码为空");
        }
        if(!checkStr(jsonObject.getString("rightCode"))){
            return BaseResult.getFailureMessageEntity("右编码为空");
        }*/
        List<String> fzhs = Arrays.asList(new String[]{"1", "2", "6", "73", "D01" , "jobass" , "93" , "96", "22"});
        if(!fzhs.contains(jsonObject.getString("archivesType"))){
            return BaseResult.getFailureMessageEntity("档案类型错误，请联系管理员");
        }
        GeneralDataEntity generalDataEntity = jsonObject.toJavaObject(GeneralDataEntity.class);
        try {
            List<GeneralDataEntity> generalDataEntities = generalDataDao.queryGeneralData(generalDataEntity);
            PageInfo pageInfo=new PageInfo(generalDataEntities);
            return BaseResult.getSuccessMessageEntity("查询成功",pageInfo);
        }catch (Exception e){
            logger.info("档案对照查询错误：{}",e.getMessage());
            return BaseResult.getFailureMessageEntity("查询失败");
        }
    }

    //编辑档案对照
    @Override
    public JsonResultEntity updateEntity(JSONObject json) {
        JSONObject jsonObject = json.getJSONObject("jsonStr");
        if(jsonObject == null ){
            return BaseResult.getFailureMessageEntity("数据为空，请先传递数据");
        }
        if(!checkStr(jsonObject.getString("id"))){
            return BaseResult.getFailureMessageEntity("id为空");
        }
        if(!checkStr(jsonObject.getString("archivesType"))){
            return BaseResult.getFailureMessageEntity("档案对照类型为空");
        }
        /*if(!checkStr(jsonObject.getString("leftCode"))){
            return BaseResult.getFailureMessageEntity("左编码为空");
        }
        if(!checkStr(jsonObject.getString("rightCode"))){
            return BaseResult.getFailureMessageEntity("右编码为空");
        }
        if(!checkStr(jsonObject.getString("leftAppCode"))){
            return BaseResult.getFailureMessageEntity("左应用编码为空");
        }
        if(!checkStr(jsonObject.getString("rightAppCode"))){
            return BaseResult.getFailureMessageEntity("右应用编码为空");
        }*/
        List<String> fzhs = Arrays.asList(new String[]{"1", "2", "6", "73", "D01" , "jobass" , "93" , "96", "22"});
        if(!fzhs.contains(jsonObject.getString("archivesType"))){
            return BaseResult.getFailureMessageEntity("档案类型错误，请联系管理员");
        }
        GeneralDataEntity generalDataEntity = jsonObject.toJavaObject(GeneralDataEntity.class);
        try {
            generalDataDao.updateGeneralDataByType(generalDataEntity);
            return BaseResult.getSuccessMessageEntity("编辑成功");
        }catch (Exception e){
            logger.info("档案对照编辑失败:{}",e.getMessage());
            return BaseResult.getFailureMessageEntity("编辑失败");
        }
    }

    //删除档案对照
    @Override
    public JsonResultEntity deleteEntity(JSONObject json) {
        JSONObject jsonObject = json.getJSONObject("jsonStr");
        if(jsonObject == null ){
            return BaseResult.getFailureMessageEntity("数据为空，请先传递数据");
        }
        if(!checkStr(jsonObject.getString("id"))){
            return BaseResult.getFailureMessageEntity("id为空");
        }
        if(!checkStr(jsonObject.getString("archivesType"))){
            return BaseResult.getFailureMessageEntity("档案对照类型为空");
        }
        /*if(!checkStr(jsonObject.getString("leftCode"))){
            return BaseResult.getFailureMessageEntity("左编码为空");
        }
        if(!checkStr(jsonObject.getString("rightCode"))){
            return BaseResult.getFailureMessageEntity("右编码为空");
        }
        if(!checkStr(jsonObject.getString("leftAppCode"))){
            return BaseResult.getFailureMessageEntity("左应用编码为空");
        }
        if(!checkStr(jsonObject.getString("rightAppCode"))){
            return BaseResult.getFailureMessageEntity("右应用编码为空");
        }*/
        List<String> fzhs = Arrays.asList(new String[]{"1", "2", "6", "73", "D01" , "jobass" , "93" , "96", "22"});
        if(!fzhs.contains(jsonObject.getString("archivesType"))){
            return BaseResult.getFailureMessageEntity("档案类型错误，请联系管理员");
        }
        GeneralDataEntity generalDataEntity = jsonObject.toJavaObject(GeneralDataEntity.class);
        try {
            generalDataEntity.setCreate_user_id("1");
            generalDataEntity.setModify_user_id("1");
           generalDataDao.deleteGeneralDataByType(generalDataEntity);
            return BaseResult.getSuccessMessageEntity("删除成功");
        }catch (Exception e){
            logger.info("档案对照删除失败:{}",e.getMessage());
            return BaseResult.getFailureMessageEntity("删除失败");
        }
    }

    //查询单个档案对照对象
    @Override
    public JsonResultEntity getEntity(JSONObject json) {
        JSONObject jsonObject = json.getJSONObject("jsonStr");
        if(jsonObject == null ){
            return BaseResult.getFailureMessageEntity("数据为空，请先传递数据");
        }
        if(!checkStr(jsonObject.getString("id"))){
            return BaseResult.getFailureMessageEntity("id为空");
        }
        GeneralDataEntity generalDataEntity = jsonObject.toJavaObject(GeneralDataEntity.class);
        try {
            GeneralDataEntity generalDataEntityOne = generalDataDao.queryOne(generalDataEntity);
            return BaseResult.getSuccessMessageEntity("查询单个成功",generalDataEntityOne);
        }catch (Exception e){
            logger.info("查询单个档案对照错误:{}",e.getMessage());
            return BaseResult.getSuccessMessageEntity("查询单个失败");
        }
    }
}
