package com.casic.missiles.modular.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.casic.missiles.core.application.context.AbstractPermissionContext;
import com.casic.missiles.core.util.SpringContextHolder;
import com.casic.missiles.model.response.ResponseData;
import com.casic.missiles.modular.system.dao.DeptPermissionConfigDao;
import com.casic.missiles.modular.system.model.DeptPermission;
import com.casic.missiles.modular.system.service.DeptPermissionConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

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

@Slf4j
@Service
public class DeptPermissionConfigServiceImpl extends ServiceImpl<DeptPermissionConfigDao, DeptPermission> implements DeptPermissionConfigService {

    private final AbstractPermissionContext permissionService;

    public DeptPermissionConfigServiceImpl(AbstractPermissionContext permissionService) {
        this.permissionService = permissionService;
    }

    public ResponseData deviceDeptPermissionList(String deptid) {
        ResponseData responseData = new ResponseData();
        try {
            List<Map<String, Object>> deptList = this.baseMapper.getListPage(deptid);
            this.supplementData(deptList);
            responseData.setMessage("查询成功");
            responseData.setCode(200);
            responseData.setData(deptList);
        } catch (DataAccessException dex) {
            log.error("主题：组织权限配置查询出现异常，组织部门编号{},异常信息为{}", deptid, dex);
            responseData.setCode(500);
            responseData.setMessage("数据库异常");
        }
        return responseData;
    }

    public Object addDeptPermission(DeptPermission deptPermission) {
        ResponseData responseData = new ResponseData();
        try {
            this.baseMapper.insert(deptPermission);
            responseData.setMessage("新增成功");
            responseData.setCode(200);
        } catch (DataAccessException dex) {
            log.error("主题：组织权限配置新增出现异常，主键id:{},井类型:{},设备类型{},异常信息为{}",
                    deptPermission.getDeptid(), deptPermission.getWellType(), deptPermission.getDeviceType(), dex);
            responseData.setCode(500);
            responseData.setMessage("数据库异常");
        }
        return responseData;
    }

    public Object editDeptPermission(DeptPermission deviceImei) {
        ResponseData responseData = new ResponseData();
        if (ObjectUtil.isEmpty(deviceImei.getId())) {
            responseData.setMessage("主键不能为空");
            responseData.setCode(200);
        } else {
            try {
                this.baseMapper.updateById(deviceImei);
                responseData.setMessage("更新成功");
                responseData.setCode(200);
            } catch (DataAccessException dex) {
                log.error("主题：组织权限配置更新出现异常，主键id:{},异常信息为{}", deviceImei.getId(), dex);
                responseData.setCode(500);
                responseData.setMessage("数据库异常");
            }
        }
        return responseData;
    }

    public Object deleteDeptPermission(Long id) {
        ResponseData responseData = new ResponseData();
        try {
            this.baseMapper.deleteById(id);
            responseData.setMessage("删除成功");
            responseData.setCode(200);
        } catch (DataAccessException dex) {
            log.error("主题：组织权限配置删除出现异常，主键id:{},异常信息为{}", id, dex);
            responseData.setCode(500);
            responseData.setMessage("数据库异常");
        }
        return responseData;
    }

    private void supplementData(List<Map<String, Object>> deptList) {
        deptList.forEach(
                dept -> {
                    dept.put("deptName", permissionService.getDeptService().getDeptName(Long.valueOf(dept.get("deptid").toString())));
                    dept.put("wellTypeName", dept.get("wellType") == null ? "" : getWellTypeList(dept.get("wellType").toString()));
                    dept.put("wellType", dept.get("wellType")==null?"":JSON.parseObject(dept.get("wellType").toString(), List.class));
                    dept.put("deviceTypeName", dept.get("deviceType") == null ? "" : getDevcieTypeList(dept.get("deviceType").toString()));
                    dept.put("deviceType",  dept.get("deviceType") == null ? "" :JSON.parseObject(dept.get("deviceType").toString(), List.class));
                }
        );
    }

    private String getWellTypeList(String wellTypes) {
        Long pid = this.baseMapper.getPid();
        StringBuffer wellTypeNames = new StringBuffer();
        for (Object wellType : JSON.parseObject(wellTypes, List.class)) {
            wellTypeNames.append(this.baseMapper.getWellName(pid, wellType.toString()) + ",");
        }
        wellTypeNames.deleteCharAt(wellTypeNames.length() - 1);
        return wellTypeNames.toString();
    }

    private String getDevcieTypeList(String devcieTypes) {
        StringBuffer devcieTypeNames = new StringBuffer();
        for (Object devcieType : JSON.parseObject(devcieTypes, List.class)) {
            devcieTypeNames.append(this.baseMapper.getDeviceName(Long.valueOf(devcieType.toString())) + ",");
        }
        devcieTypeNames.deleteCharAt(devcieTypeNames.length() - 1);
        return devcieTypeNames.toString();
    }

}
