package com.yihu.jw.base.service.dict;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.jw.base.dao.dict.BusinessSysDictDao;
import com.yihu.jw.entity.care.common.BusinessSysDictDO;
import com.yihu.jw.restmodel.web.Envelop;
import com.yihu.jw.restmodel.web.MixEnvelop;
import com.yihu.jw.utils.hibernate.HibenateUtils;
import com.yihu.mysql.query.BaseJpaService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yeshijie
 * @Date: 2021/5/10
 * @Description:
 */
@Service
public class BusinessSysDictService extends BaseJpaService<BusinessSysDictDO, BusinessSysDictDao> {

    @Autowired
    private HibenateUtils hibenateUtils;
    @Autowired
    private ObjectMapper objectMapper;
    @Autowired
    private BusinessSysDictDao businessSysDictDao;

    //根据id修改或新增字典
    public BusinessSysDictDO updateOrCreateDict(String json) throws  Exception{
        BusinessSysDictDO  businessSysDictDO = objectMapper.readValue(json, BusinessSysDictDO.class);
        String modelName = "";
        if (StringUtils.isNoneBlank(businessSysDictDO.getDictName())){
            List<BusinessSysDictDO> modelNameList=businessSysDictDao.findByDictName(businessSysDictDO.getDictName());
            if (modelNameList!=null&&modelNameList.size()>0){
                modelName= modelNameList.get(0).getModelName();
            }

        }
        businessSysDictDO.setModelName(modelName);
        if(StringUtils.isNoneBlank(businessSysDictDO.getId())){
            BusinessSysDictDO businessSysDictDO1 = businessSysDictDao.findOne(businessSysDictDO.getId());
            if (businessSysDictDO1!=null){
                businessSysDictDO1.setDictValue(businessSysDictDO.getDictValue());
                businessSysDictDO1.setDictCode(businessSysDictDO.getDictCode());
                businessSysDictDO1.setDictName(businessSysDictDO.getDictName());
                businessSysDictDO1.setPyCode(businessSysDictDO.getPyCode());
                businessSysDictDO1.setImgUrl(businessSysDictDO.getImgUrl());
                businessSysDictDO1.setSort(businessSysDictDO.getSort());
                businessSysDictDO1.setExt1(businessSysDictDO.getExt1());
                businessSysDictDO1.setExt2(businessSysDictDO.getExt2());
                businessSysDictDO1.setOrgCode(businessSysDictDO.getOrgCode());
                businessSysDictDO1.setOrgName(businessSysDictDO.getOrgName());
                businessSysDictDO1.setModelName(modelName);
            }else {
                businessSysDictDO1 = new BusinessSysDictDO();
                businessSysDictDO1.setDictValue(businessSysDictDO.getDictValue());
                businessSysDictDO1.setDictCode(businessSysDictDO.getDictCode());
                businessSysDictDO1.setDictName(businessSysDictDO.getDictName());
                businessSysDictDO1.setPyCode(businessSysDictDO.getPyCode());
                businessSysDictDO1.setImgUrl(businessSysDictDO.getImgUrl());
                businessSysDictDO1.setSort(businessSysDictDO.getSort());
                businessSysDictDO1.setExt1(businessSysDictDO.getExt1());
                businessSysDictDO1.setExt2(businessSysDictDO.getExt2());
                businessSysDictDO1.setOrgCode(businessSysDictDO.getOrgCode());
                businessSysDictDO1.setOrgName(businessSysDictDO.getOrgName());
                businessSysDictDO1.setModelName(modelName);
            }

            return businessSysDictDao.save(businessSysDictDO1);
        }
        return businessSysDictDao.save(businessSysDictDO);
    }

    //根据id删除字典
    public String  deleteDictById(String id){
        String msg = "";
        if (StringUtils.isNoneBlank(id)){
            BusinessSysDictDO businessSysDictDO = businessSysDictDao.findOne(id);
            if (businessSysDictDO!=null) {
                businessSysDictDao.delete(id);
                msg = "删除成功";
            }else {
                msg = "没有这条记录";
            }
        }else{
            msg = "id为空";
        }
        return msg;
    }

    //查字典
    public MixEnvelop findDictsByNameCode(String modelName,String name,String code,String value,Integer page ,Integer pageSize){
        String  sql = "select t.id as \"id\"," +
                " t.dict_name  as \"dictName\","+
                " t.model_name  as \"modelName\","+
                " t.dict_code  as \"dictCode\","+
                " t.dict_value   as \"dictValue\","+
                " t.sort   as \"sort\","+
                " t.py_code  as \"pyCode\","+
                " t.img_url  as \"imgUrl\","+
                " t.org_code  as \"orgCode\","+
                " t.org_name  as \"orgName\","+
                " t.create_time as \"createTime\""+
                " , t.ext1, t.ext2 from base_business_sys_dict t where 1=1 ";
        if (StringUtils.isNoneBlank(name)){
            sql+=" and t.dict_name like '%"+name+"%'";
        }
        if (StringUtils.isNoneBlank(modelName)){
            sql+=" and t.model_name like '%"+modelName+"%'";
        }
        if (StringUtils.isNoneBlank(code)){
            sql+=" and t.dict_code like '%"+code+"%'";
        }
        if (StringUtils.isNoneBlank(value)){
            sql+=" and t.dict_value like '%"+value+"%'";
        }
        sql+=" and t.dict_code is not null and  t.dict_code != ''";
        if (page==null||page==null){
            List<Map<String,Object>> list = hibenateUtils.createSQLQuery(sql);
            MixEnvelop mixEnvelop = new MixEnvelop();
            mixEnvelop.setTotalCount(list==null?0:list.size());
            mixEnvelop.setDetailModelList(list);
            return  mixEnvelop;
        }else {
            List<Map<String,Object>> list = hibenateUtils.createSQLQuery(sql,page,pageSize);
            List<Map<String,Object>> listCount = hibenateUtils.createSQLQuery(sql);
            MixEnvelop mixEnvelop = new MixEnvelop();
            mixEnvelop.setTotalCount(listCount==null?0:listCount.size());
            mixEnvelop.setCurrPage(page);
            mixEnvelop.setPageSize(pageSize);
            mixEnvelop.setDetailModelList(list);
            return  mixEnvelop;
        }

    }

    //查字典
    public MixEnvelop findModel(String modelName, String name, Integer page , Integer pageSize){
        MixEnvelop mixEnvelop = new MixEnvelop();
        String  sql = "select " +
                " t.dict_name  as \"dictName\","+
                " t.model_name  as \"modelName\""+
                " from base_business_sys_dict t where 1=1 ";
        if (StringUtils.isNoneBlank(name)){
            sql+=" and t.dict_name like '%"+name+"%'";
        }
        if (StringUtils.isNoneBlank(modelName)){
            sql+=" and t.model_name like '%"+modelName+"%'";
        }
        sql+=" group by t.dict_name,t.model_name ";
        List<Map<String,Object>> list = hibenateUtils.createSQLQuery(sql,page,pageSize);
        List<Map<String,Object>> listCount = hibenateUtils.createSQLQuery(sql);

        if (listCount!=null){
            mixEnvelop.setTotalCount(listCount.size());
        }else {
            mixEnvelop.setTotalCount(0);
        }
        mixEnvelop.setDetailModelList(list);
        mixEnvelop.setCurrPage(page);
        mixEnvelop.setPageSize(pageSize);
        return  mixEnvelop;
    }

    public Envelop updateByModelName(String jsonData) throws Exception{
        Envelop envelop = new Envelop();
        JSONObject jsonObject = JSONObject.parseObject(jsonData);
        if (StringUtils.isNoneBlank(jsonObject.getString("preDictName"))){
            String preDictName = jsonObject.get("preDictName").toString();
            List<BusinessSysDictDO> list  =  businessSysDictDao.findByDictName(preDictName);
            for (BusinessSysDictDO sysDictDO:list){
                if (jsonObject.get("dictName")!=null){
                    sysDictDO.setDictName(jsonObject.get("dictName").toString());
                }
                if (jsonObject.get("modelName")!=null){
                    sysDictDO.setModelName(jsonObject.get("modelName").toString());
                }
            }
            businessSysDictDao.save(list);
            envelop.setMessage("操作成功");
            envelop.setStatus(200);
        }else {
            if (jsonObject.get("dictName")!=null){
                List<BusinessSysDictDO> list  =  businessSysDictDao.findByDictName(jsonObject.get("dictName").toString());
                if (list!=null&&list.size()>0){
                    throw new Exception("该dictName已经存在");
                }else {
                    BusinessSysDictDO sysDictDO=new BusinessSysDictDO();
                    sysDictDO.setDictName(jsonObject.get("dictName").toString());
                    if (jsonObject.get("modelName")!=null){
                        sysDictDO.setModelName(jsonObject.get("modelName").toString());
                    }
                    businessSysDictDao.save(sysDictDO);
                }
                envelop.setMessage("操作成功");
                envelop.setStatus(200);

            }else {
                envelop.setMessage("dictName为空");
                envelop.setStatus(-1);
            }
        }
        return envelop;
    }

    public Envelop deleteByModelName(String preDictName){
        Envelop envelop = new Envelop();
        if (StringUtils.isNoneBlank(preDictName)){
            List<BusinessSysDictDO> list  =  businessSysDictDao.findByDictName(preDictName);
            for (BusinessSysDictDO sysDictDO:list){
                businessSysDictDao.delete(sysDictDO);
            }
            envelop.setMessage("操作成功");
            envelop.setStatus(200);
        }else {
            envelop.setMessage("preDictName为空");
            envelop.setStatus(-1);
        }
        return envelop;
    }
}
