package com.dxhy.manage.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.utils.PageUtils;
import com.dxhy.common.utils.Query;
import com.dxhy.manage.dao.SysInterfaceDao;
import com.dxhy.manage.entity.SysDeptEntity;
import com.dxhy.manage.entity.SysInterfaceAuthority;
import com.dxhy.manage.entity.SysInterfaceInfo;
import com.dxhy.manage.service.SysDeptService;
import com.dxhy.manage.service.SysInterfaceInfoService;
import com.dxhy.manage.service.SysInterfaceService;
import org.apache.commons.lang.StringUtils;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class SysInterfaceServiceImpl extends ServiceImpl<SysInterfaceDao, SysInterfaceAuthority>
    implements SysInterfaceService {

    @Resource
    private SysInterfaceInfoServiceImpl sysInterfaceInfoService;
    @Resource
    private SysDeptService sysDeptService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        String deptId = (String)params.get("deptId");
        String interfaceCode = (String)params.get("interfaceCode");
        String interfaceName = (String)params.get("interfaceName");
        IPage<SysInterfaceAuthority> page = this.page(new Query<SysInterfaceAuthority>().getPage(params),
            new QueryWrapper<SysInterfaceAuthority>().eq(StringUtils.isNotBlank(deptId), "dept_id", deptId)
                .like(StringUtils.isNotBlank(interfaceCode), "interface_code", interfaceCode)
                .like(StringUtils.isNotBlank(interfaceName), "interface_name", interfaceName));
        return new PageUtils(page);
    }

    @Override
    public void deleteInterface(long id) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        this.removeById(id);
    }

    @Override
    public PageUtils unAuthList(Map<String, Object> params) {
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        String deptId = (String)params.get("deptId");
        List<String> codes = new ArrayList<>();
        IPage<SysInterfaceInfo> page = null;
        List<SysInterfaceAuthority> infoList =
            this.list(new QueryWrapper<SysInterfaceAuthority>().eq("dept_id", deptId));
        if (infoList != null && infoList.size() > 0) {
            for (SysInterfaceAuthority info : infoList) {
                codes.add(info.getInterfaceCode());
            }
            page = sysInterfaceInfoService.page(new Query<SysInterfaceInfo>().getPage(params),
                new QueryWrapper<SysInterfaceInfo>().notIn("interface_code", codes));
        } else {
            page = sysInterfaceInfoService.page(new Query<SysInterfaceInfo>().getPage(params),
                new QueryWrapper<SysInterfaceInfo>());
        }
        return new PageUtils(page);
    }

    @Override
    public void insertCompanyAuth(Map<String, Object> params) {
        JSONObject json = new JSONObject(params);
        JSONArray codes = json.getJSONArray("interfaceCode");
        String deptId = json.getString("deptId");
        String name = json.getString("name");
        String companyCode = (String)params.get("company");
        List<SysInterfaceAuthority> list = new ArrayList<>();
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        List<SysInterfaceInfo> listInfo = sysInterfaceInfoService.list();
        for (Object code : codes) {
            SysInterfaceAuthority sysInterfaceAuthority = new SysInterfaceAuthority();
            sysInterfaceAuthority.setInterfaceCode(code.toString());
            sysInterfaceAuthority.setCompanyName(name);
            sysInterfaceAuthority.setDeptId(deptId);
            sysInterfaceAuthority.setCreateDate(new Date());
            sysInterfaceAuthority.setCompanyCode(companyCode);
            for (SysInterfaceInfo info : listInfo) {
                if (code.equals(info.getInterfaceCode())) {
                    sysInterfaceAuthority.setInterfaceName(info.getInterfaceName());
                }
            }
            list.add(sysInterfaceAuthority);
        }
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        this.saveBatch(list);
    }

    @Override
    public void insertInterface(SysInterfaceInfo sysInterfaceInfo) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        sysInterfaceInfoService.save(sysInterfaceInfo);
    }

    @Override
    public PageUtils listInterfaceInfo(Map<String, Object> params) {
        String interfaceCode = (String)params.get("interfaceCode");
        String interfaceName = (String)params.get("interfaceName");
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        return new PageUtils(sysInterfaceInfoService.page(new Query<SysInterfaceInfo>().getPage(params),
            new QueryWrapper<SysInterfaceInfo>()
                .like(StringUtils.isNotBlank(interfaceCode), "interface_code", interfaceCode)
                .like(StringUtils.isNotBlank(interfaceName), "interface_name", interfaceName)));
    }

    @Override
    public void deleteInterfaceByid(long id) {
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        sysInterfaceInfoService.removeById(id);
    }

    @Override
    public void insertBatchAuth(Map<String, Object> params) {
        JSONObject json = new JSONObject(params);
        String interfaceCode = json.getString("interfaceCode");
        String interfaceName = json.getString("interfaceName");
        JSONArray deptIds = json.getJSONArray("deptId");
        List<SysInterfaceAuthority> list = new ArrayList<>();
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        List<SysDeptEntity> deptList = sysDeptService.list(new QueryWrapper<SysDeptEntity>().in("dept_id", deptIds));
        for (Object deptId : deptIds) {
            SysInterfaceAuthority sysInterfaceAuthority = new SysInterfaceAuthority();
            sysInterfaceAuthority.setInterfaceCode(interfaceCode);
            sysInterfaceAuthority.setInterfaceName(interfaceName);
            sysInterfaceAuthority.setDeptId(deptId.toString());
            sysInterfaceAuthority.setCreateDate(new Date());
            for (SysDeptEntity dept : deptList) {
                if (deptId.equals(String.valueOf(dept.getDeptId()))) {
                    sysInterfaceAuthority.setCompanyCode(dept.getCompany());
                    sysInterfaceAuthority.setCompanyName(dept.getName());
                }
            }
            list.add(sysInterfaceAuthority);
        }
        DynamicContextHolder.push(DbConstant.BASICS_WRITE);
        this.saveBatch(list);
    }

    @Override
    public PageUtils unDeptList(Map<String, Object> params) {
        String interfaceCode = (String)params.get("interfaceCode");
        List<Long> deptIds = new ArrayList<>();
        IPage<SysDeptEntity> page = null;
        List<SysInterfaceAuthority> list =
            this.list(new QueryWrapper<SysInterfaceAuthority>().eq("interface_code", interfaceCode));
        if (list != null && list.size() > 0) {
            for (SysInterfaceAuthority auth : list) {
                deptIds.add(Long.parseLong(auth.getDeptId()));
            }
            page = sysDeptService.page(new Query<SysDeptEntity>().getPage(params),
                new QueryWrapper<SysDeptEntity>().eq("org_type", "1").eq("is_model", "1").notIn("dept_id", deptIds));
        } else {
            page = sysDeptService.page(new Query<SysDeptEntity>().getPage(params),
                new QueryWrapper<SysDeptEntity>().eq("org_type", "1").eq("is_model", "1"));
        }
        return new PageUtils(page);
    }
}
