package com.xhpc.general.service;

import com.xhpc.common.core.web.domain.AjaxResult;
import com.xhpc.common.domain.XhpcDictBiz;
import com.xhpc.general.domain.XhpcDictionary;
import com.xhpc.general.mapper.XhpcDictBizMapper;
import com.xhpc.general.mapper.XhpcDictBizQuantityCheckMapper;
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.List;

/**
 * 业务字典Service业务层处理
 *
 * @author yuyang
 * @date 2021-07-19
 */
@Service
public class XhpcDictBizServiceImpl implements IXhpcDictBizService{

    @Autowired
    private XhpcDictBizMapper xhpcDictBizMapper;

    @Autowired
    private XhpcDictBizQuantityCheckMapper xhpcDictBizQuantityCheckMapper;

    @Override
    public List<XhpcDictBiz> getCode(String code) {
        return xhpcDictBizMapper.getCode(code);
    }


    @Transactional
    @Override
    public AjaxResult addADictionaryItem(String name, String code, Integer sort) {

        //Ensure the parameter is not null.
        if(name==null){
            return AjaxResult.error("字典名称不能为空");
        }
        if(code==null){
            return AjaxResult.error("字典编码不能为空");
        }
        if(sort==null){
            return AjaxResult.error("字典排序不能为空");
        }
        //Ensuring the sort's size is bigger than 0
        if(sort<1){
            return AjaxResult.error("字典排序必须大于0");
        }
        //Ensuring the length of parameter's is correct.
        if(name.length()>255){
            return AjaxResult.error("字典名称的长度不能大于255");
        }
        if(code.length()>255){
            return AjaxResult.error("字典编码的长度不能大于255");
        }
        if(String.valueOf(sort).length()>11){
            return AjaxResult.error("字典排序的长度不能大于11");
        }


        //Ensuring the code whether has existed.
        int quantity=xhpcDictBizQuantityCheckMapper.countCodeRows(code);
        if(quantity>0){
            return AjaxResult.error("您输入的字符编码已经存在");
        }

        //Ensuring the name(dict_value) has existed.
        int size=xhpcDictBizMapper.countParentNameQuantity(name);
        if(size>0){
            return AjaxResult.error("您输入的字符名称已经存在");
        }else {
            xhpcDictBizMapper.insertAParentItemToTable(name,code,sort);
        }
        return AjaxResult.success();
    }

    @Override
    @Transactional
    public AjaxResult selectDictionaryItem(Long id) {
        XhpcDictionary xhpcDictionary=xhpcDictBizMapper.selectADictionaryItem(id);
        if(xhpcDictionary==null){
            return AjaxResult.error("该字典不存在");
        }
        return AjaxResult.success(xhpcDictionary);
    }

    @Override
    @Transactional
    public AjaxResult addAChildDictionaryItem(Long parentId, String code, String dictKey, String dictValue,Integer sort) {


        if(code==null){
            return AjaxResult.error("字典编码不能为空");
        }
        code=code.trim();
        if(code.equals("")){
            return AjaxResult.error("字典编码不能为空");
        }
        int keySize=xhpcDictBizQuantityCheckMapper.countSameParentChildKeyColumns(parentId,dictKey);
        if(keySize>0){
            return AjaxResult.error("您输入的字典键值已经存在");
        }
        //Ensuring the parameter is not null.
        if(parentId==null){
            return AjaxResult.error("The parentId is null");
        }
        if(dictKey==null){
            return AjaxResult.error("字典键值不能为空");
        }
        dictKey=dictKey.trim();
        if(dictKey.equals("")){
            return AjaxResult.error("字典键值不能为空");
        }
        if(dictValue==null){
            return AjaxResult.error("字典名称不能为空");
        }
        dictValue=dictValue.trim();
        if(dictValue.equals("")){
            return AjaxResult.error("字典名称不能为空");
        }
        if(sort==null){
            return AjaxResult.error("字典排序不能为空");
        }
        //Ensuring sort is bigger than 0;
        if(sort<1){
            return AjaxResult.error("字典排序的值不能小于1");
        }
        //Ensuring the length of parameter's is correct.
        if(dictKey.length()>255){
            return AjaxResult.error("字典键值的长度不能大于255");
        }
        if(dictValue.length()>255){
            return AjaxResult.error("字典名称的长度不能大于255");
        }
        if(String.valueOf(sort).length()>11){
            return AjaxResult.error("字典排序的长度不能大于11");
        }
        //Ensuring value is unique.
        int size=xhpcDictBizMapper.countSameParentChildNameColumns(parentId,dictValue);
        int sizeFather=xhpcDictBizQuantityCheckMapper.countParentName(dictValue);
        if(size>0||sizeFather>0){
            return AjaxResult.error("你输入的字典名称已经存在");
        }else {
            xhpcDictBizMapper.insertAChildItemToTable(parentId, code, dictKey, dictValue,sort);
        }
        return AjaxResult.success();
    }

    @Override
    @Transactional
    public AjaxResult deleteChildDictionaryItem(Long id) {
        int length=xhpcDictBizMapper.deleteChildDictionaryItemById(id);
        if(length==0){
            return AjaxResult.error("not exists");
        }
        return AjaxResult.success();
    }


    @Override
    @Transactional
    public AjaxResult deleteParentItem(Long id) {


        try {
            xhpcDictBizMapper.deleteChildDictionaryItemById(id);
        } catch (Exception e) {
            return AjaxResult.error("删除失败");
        }

        try {
            //Deleting their child.
            xhpcDictBizMapper.deleteChildItemsFollowParent(id);
        } catch (Exception e) {
            return AjaxResult.error("删除失败");
        }
        return  AjaxResult.success();
    }

    @Override
    @Transactional
    public List<XhpcDictionary> getMainItems(String code, String name) {

        return xhpcDictBizMapper.selectParentDictionaryItemsByCodeAndName(code,name);
    }

    @Override
    @Transactional
    public List<XhpcDictionary> getDetailsItems(Long parentId,String code, String name) {

        return xhpcDictBizMapper.selectChildDictionaryItemsByCodeAndName(parentId,code,name);
    }


    @Override
    @Transactional
    public AjaxResult updateMainItem(Long id,String code, String dictValue, Integer sort) {

        //Ensuring the code is valid, it is not a null value or a white value.
        if(code==null){
            return AjaxResult.error("字典编码不能为空");
        }
        code=code.trim();
        if(code.equals("")){
            return AjaxResult.error("字典编码不能为空");
        }
        if(code.length()>255){
            return  AjaxResult.error("字符编码长度不能大于255");
        }
        //Ensuring the sort is bigger than zero.
        if(sort<1){
            return AjaxResult.error("字典排序的值不能小于1");
        }
        if(String.valueOf(sort).length()>11){
            return AjaxResult.error("字典排序长度不能大于11");
        }

        if(dictValue==null){
            return AjaxResult.error("字典名称不能为空");
        }
        dictValue=dictValue.trim();
        if(dictValue.equals("")){
            return AjaxResult.error("字典名称不能为空");
        }
        if(dictValue.length()>255){
            return AjaxResult.error("字符名称不能大于255");
        }
        //Ensuring the code and dict_value is unique.
        XhpcDictionary xhpcDictionary=xhpcDictBizMapper.selectADictionaryItem(id);
        String value=xhpcDictionary.getDictValue();
        String codeValue=xhpcDictionary.getCode();

//        Judging the father item whether could insert.
        if(value!=null&&!value.equals(dictValue)){
            //Ensuring the name(dict_value) has existed.
            int size=xhpcDictBizMapper.countParentNameQuantity(dictValue);
            if(size>0){
                return AjaxResult.error("您输入的字典名称已经存在。");
            }
        }
        if(codeValue!=null&&!codeValue.equals(code)){
            //Ensuring the code whether has existed.
            int quantity=xhpcDictBizQuantityCheckMapper.countCodeRows(code);
            if(quantity>0){
                return AjaxResult.error("您输入的字典编号已经存在。");
            }
        }
        xhpcDictBizMapper.updateParentItemByCodeDictValueSort(id, code, dictValue, sort);
        //在父级修改了字典编号后，子级也要修改字字典编号与父级保持一致。
        xhpcDictBizMapper.updateChildCodeAfterParentChanged(code,id);
        return AjaxResult.success();
    }

    @Override
    @Transactional
    public AjaxResult updateChildItem(Long parentId,Long id, String value, String key,Integer sort) {
        //Ensuring the value is valid.
        if(value==null){
            return AjaxResult.error("字典名称不能为空");
        }
        value=value.trim();
        if(value.equals("")){
            return AjaxResult.error("字典名称不能为空");
        }
        if(value.length()>255){
            return AjaxResult.error("字符名称长度不能大于255");
        }
        //Ensuring the key is valid.
        if(key==null){
            return AjaxResult.error("字典键值不能为空");
        }
        key=key.trim();
        if(key.equals("")){
            return AjaxResult.error("字典键值不能为空");
        }
        if(key.length()>255){
            return AjaxResult.error("字符键值不能为空");
        }
        if(sort<1){
            return AjaxResult.error("字典排序不能小于1");
        }
        if(String.valueOf(sort).length()>11){
            return AjaxResult.error("字典排序长度不能大于11");
        }
        XhpcDictionary xhpcDictionary=xhpcDictBizMapper.selectADictionaryItem(id);
        System.out.println(xhpcDictionary.getDictKey());
        //Ensuring the key and value is unique.
        if(!xhpcDictionary.getDictKey().equals(key)){
            int keySize=xhpcDictBizQuantityCheckMapper.countSameParentChildKeyColumns(parentId,key);
            System.out.println(keySize);
            if(keySize!=0){
                return AjaxResult.error("您输入的字典键值已经存在。");
            }
        }
        System.out.println(xhpcDictionary.getDictValue());
        if(!xhpcDictionary.getDictValue().equals(value)){
            int valueSize=xhpcDictBizQuantityCheckMapper.countSameParentChildValueColumns(parentId,value);
            System.out.println(valueSize);
            if(valueSize!=0){
                return AjaxResult.error("您输入的字典名称已经存在");
            }
        }
        int i=xhpcDictBizMapper.updateChildItem(id,value,key,sort);
        if(i==0){
            return AjaxResult.error("更新失败");
        }else {
            return AjaxResult.success();
        }

    }


    @Override
    @Transactional
    public AjaxResult deleteChildItems(String ids) {
        //Making the String ids into List<Long> ids.
        List<Long> idsList=new ArrayList<>();
        String [] idsArray=ids.split(",");
        for(String value:idsArray){
            idsList.add(Long.parseLong(value));
        }

        int size=xhpcDictBizMapper.deleteChildItemByIds(idsList);
        if(size==0){
            return AjaxResult.error("删除失败");
        }
        return AjaxResult.success();
    }

    @Override
    @Transactional
    public AjaxResult deleteParentItems(String ids) {
        //Making the String ids into List<Long> ids.
        List<Long> idsList=new ArrayList<>();
        String [] idsArray=ids.split(",");
        for(String value:idsArray){
            idsList.add(Long.parseLong(value));
        }
        //Deleting parent first.
        int sizeParent=xhpcDictBizMapper.deleteChildItemByIds(idsList);
        if(sizeParent==0){
            return AjaxResult.error("删除失败");
        }

        //Deleting the parent's child.
        try {
            xhpcDictBizMapper.deleteChildItemsAfterTheirParentDied(idsList);
        } catch (Exception e) {
            return AjaxResult.error("删除失败");
        }

        return AjaxResult.success();
    }
}
