package com.gree.mall.manager.logic.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminDeptTree;
import com.gree.mall.manager.bean.admin.AdminDeptWebsitVO;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.bean.admin.AdminWebsitTree;
import com.gree.mall.manager.bean.workorder.IncreVO;
import com.gree.mall.manager.commonmapper.AdminMapper;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.*;
import com.gree.mall.manager.plus.service.AdminDeptService;
import com.gree.mall.manager.plus.service.AdminDeptWebsitService;
import com.gree.mall.manager.plus.service.AdminUserDeptRelaService;
import com.gree.mall.manager.plus.service.AdminWebsitService;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class AdminDeptLogic {

    @Autowired
    CommonLogic commonLogic;

    @Autowired
    AdminDeptService adminDeptService;

    @Autowired
    AdminMapper adminMapper;

    @Autowired
    AdminDeptWebsitService adminDeptWebsitService;


    @Autowired
    AdminUserDeptRelaService adminUserDeptRelaService;

    @Autowired
    AdminWebsitService adminWebsitService;



    public List<AdminDeptTree> tree(HttpServletRequest request) {



        List<AdminDept> list = adminDeptService.lambdaQuery()
                .eq(AdminDept::getAdminDeptId,"1")
                .list();
        List<AdminDeptTree> trees = new ArrayList<>();
        for(AdminDept adminDept : list){
            AdminDeptTree adminDeptTree = new AdminDeptTree();
            BeanUtils.copyProperties(adminDept,adminDeptTree);
            trees.add(adminDeptTree);
        }

        for(AdminDeptTree adminDeptTree:trees){
            adminDeptTree.setChildren(this.treeModule(adminDeptTree.getAdminDeptId()));
        }
        return trees;
    }

    private List<AdminDeptTree> treeModule(String adminDeptId) {
        AdminUserCom adminUser = commonLogic.getAdminUser();

        //最父级资源树

        List<AdminDept> collect = adminDeptService.lambdaQuery()
                .eq(AdminDept::getPId,adminDeptId).list();

        if(collect.size() == 0){
            return new ArrayList<>();
        }

        List<AdminDeptTree> adminDeptTrees = new ArrayList<>();

        for(AdminDept bean : collect){
            AdminDeptTree adminDeptTree = new AdminDeptTree();
            BeanUtils.copyProperties(bean,adminDeptTree);
            adminDeptTree.setChildren(this.treeModule(adminDeptTree.getAdminDeptId()));

            adminDeptTrees.add(adminDeptTree);
        }
        return adminDeptTrees;
    }

    public IPage<AdminDeptWebsitVO> list(Page page, ZfireParamBean zfireParam) {

        //获取当前登录企业id
        AdminUserCom adminUser = commonLogic.getAdminUser();

        List<String> adminCompanyIds = adminUser.getCompanyWechatIds();
        //1.组装查询条件
        zfireParam = FieldUtils.supplyParam(zfireParam, AdminDeptWebsitVO.class);

        IPage<AdminDeptWebsitVO> adminDeptWebsitVOIPage = adminMapper.list(page, zfireParam);
        return adminDeptWebsitVOIPage;
    }

    public void add(AdminDept adminDept) {
        AdminDept adminDeptServiceById = adminDeptService.getById(adminDept.getPId());

        adminDept.setCompanyWechatId(adminDeptServiceById.getCompanyWechatId());
        adminDept.setCompanyWechatName(adminDeptServiceById.getCompanyWechatName());
        adminDept.insert();
    }

    public void update(AdminDept adminDept) {
        adminDept.updateById();

        adminDeptWebsitService.lambdaUpdate().eq(AdminDeptWebsit::getAdminDeptId,adminDept.getAdminDeptId())
                .set(AdminDeptWebsit::getDeptName,adminDept.getDeptName()).update();
    }

    public void del(String id) {
        if (adminDeptWebsitService.lambdaQuery().eq(AdminDeptWebsit::getAdminDeptId,id).count() > 0)
            throw new RemoteServiceException("存在绑定网点");

        if (adminUserDeptRelaService.lambdaQuery().eq(AdminUserDeptRela::getAdminDeptId,id).count() > 0) {
            throw new RemoteServiceException("存在绑定账号");
        }

        adminDeptService.removeById(id);
    }


    public void updateWebsit(AdminDeptWebsit adminDeptWebsit) {

        if (adminDeptWebsitService.lambdaQuery().eq(AdminDeptWebsit::getAdminDeptId,adminDeptWebsit.getAdminDeptId())
                .eq(AdminDeptWebsit::getWebsitId,adminDeptWebsit.getWebsitId()).count() > 0)
            return;

        adminDeptWebsit.updateById();
    }

    public void delWebsit(String adminDeptWebsitId) {
        AdminDeptWebsit adminDeptWebsit = adminDeptWebsitService.getById(adminDeptWebsitId);

        AdminDept adminDept = adminDeptService.getById(adminDeptWebsit.getAdminDeptId());
        if (adminDept.getPId().equals("1"))
            throw new RemoteServiceException("商户不能删除绑定网点");

        adminDeptWebsitService.removeById(adminDeptWebsitId);
    }

    public List<AdminDeptTree> treeWebsit(HttpServletRequest request) {

        AdminUserCom adminUser = commonLogic.getAdminUser(request);

        List<AdminDept> list = adminDeptService.lambdaQuery()
                .eq(AdminDept::getPId,"0")
                .list();
        List<AdminDeptTree> trees = new ArrayList<>();
        for(AdminDept adminDept : list){
            AdminDeptTree adminDeptTree = new AdminDeptTree();
            BeanUtils.copyProperties(adminDept,adminDeptTree);
            trees.add(adminDeptTree);
        }

        for(AdminDeptTree adminDeptTree:trees){
            adminDeptTree.setChildren(this.treeModule(adminDeptTree.getAdminDeptId()));

            List<AdminDeptTree> children = adminDeptTree.getChildren();
            for (AdminDeptTree child : children) {
                List<AdminDeptWebsit> adminDeptWebsits = adminDeptWebsitService.lambdaQuery().eq(AdminDeptWebsit::getAdminDeptId, child.getAdminDeptId()).list();

                for (AdminDeptWebsit adminDeptWebsit : adminDeptWebsits) {
                    AdminDeptTree adminDeptTreeWebsit = new AdminDeptTree();
                    adminDeptTreeWebsit.setIsWebsit(true);
                    adminDeptTreeWebsit.setAdminDeptId(adminDeptWebsit.getWebsitId());
                    adminDeptTreeWebsit.setDeptName(adminDeptWebsit.getWebsitName());

                    child.getChildren().add(adminDeptTreeWebsit);

                }
                AdminWebsit adminWebsit = adminWebsitService.lambdaQuery().eq(AdminWebsit::getName, child.getDeptName()).last("limit 1").one();

                if (adminWebsit != null){
                    child.setAdminDeptId(adminWebsit.getWebsitId());
                    child.setIsWebsit(true);
                }
            }
        }

        return trees;
    }

    public void addWebsit(String adminDeptId, List<String> websitIds) {
        AdminDept adminDept = adminDeptService.getById(adminDeptId);

        List<AdminWebsit> adminWebsits = adminWebsitService.lambdaQuery().in(AdminWebsit::getWebsitId, websitIds).list();

        for (AdminWebsit adminWebsit : adminWebsits) {
            AdminDeptWebsit adminDeptWebsit = new AdminDeptWebsit();
            adminDeptWebsit.setDeptName(adminDept.getDeptName());
            adminDeptWebsit.setAdminDeptId(adminDept.getAdminDeptId());
            adminDeptWebsit.setWebsitId(adminWebsit.getWebsitId());
            adminDeptWebsit.setWebsitName(adminWebsit.getName());

            if (adminDeptWebsitService.lambdaQuery().eq(AdminDeptWebsit::getAdminDeptId,adminDeptWebsit.getAdminDeptId())
                    .eq(AdminDeptWebsit::getWebsitId,adminWebsit.getWebsitId()).count() > 0)
                continue;

            adminDeptWebsit.insert();
        }
    }
}
