package com.zcloud.basic.info.facade;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.SingleResponse;
import com.zcloud.basic.info.api.DepartmentServiceI;
import com.zcloud.basic.info.command.convertor.DepartmentCoConvertor;
import com.zcloud.basic.info.dto.DepartmentAddCmd;
import com.zcloud.basic.info.dto.DepartmentPageQry;
import com.zcloud.basic.info.dto.DepartmentUpdateCmd;
import com.zcloud.basic.info.dto.clientobject.DepartmentCO;
import com.zcloud.basic.info.dto.clientobject.DepartmentInfoCO;
import com.zcloud.gbscommon.utils.Tools;
import com.zcloud.gbscommon.zclouddepartment.face.ZcloudDepartmentFacade;
import com.zcloud.gbscommon.zclouddepartment.request.ZcloudDepartmentAddCmd;
import com.zcloud.gbscommon.zclouddepartment.request.ZcloudDepartmentPageQry;
import com.zcloud.gbscommon.zclouddepartment.request.ZcloudDepartmentUpdateCmd;
import com.zcloud.gbscommon.zclouddepartment.response.ZcloudDepartmentInfoCo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;

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

/**
 * @author lin
 */
@DubboService
public class ZcloudDepartmentFacadeImpl implements ZcloudDepartmentFacade {

    @Resource
    private DepartmentServiceI departmentService;
    @Resource
    private DepartmentCoConvertor departmentCoConvertor;

    @Override
    public SingleResponse<ZcloudDepartmentInfoCo> getDepartmentInfoById(Long id) {
        DepartmentInfoCO info = departmentService.info(id);
        ZcloudDepartmentInfoCo zcloudDepartmentInfoCo = new ZcloudDepartmentInfoCo();
        BeanUtils.copyProperties(info, zcloudDepartmentInfoCo);
        return SingleResponse.of(zcloudDepartmentInfoCo);
    }

    @Override
    public PageResponse<ZcloudDepartmentInfoCo> pageByParentId(ZcloudDepartmentPageQry zcloudDepartmentPageQry) {
        DepartmentPageQry qry = new DepartmentPageQry();
        BeanUtils.copyProperties(zcloudDepartmentPageQry, qry);
        PageResponse<DepartmentCO> departmentCOPageResponse = departmentService.listPage(qry);
        List<ZcloudDepartmentInfoCo> zcloudPage = departmentCoConvertor.converCOsToDoubbpCOs(departmentCOPageResponse.getData());
        return PageResponse.of(zcloudPage, departmentCOPageResponse.getTotalCount(), departmentCOPageResponse.getPageSize(), departmentCOPageResponse.getPageIndex());
    }

    @Override
    public SingleResponse addDepartment(ZcloudDepartmentAddCmd zcloudDepartmentAddCmd) {
        DepartmentAddCmd addCmd = new DepartmentAddCmd();
        BeanUtils.copyProperties(zcloudDepartmentAddCmd, addCmd);
        return departmentService.add(addCmd);
    }

    @Override
    public SingleResponse editDepartment(ZcloudDepartmentUpdateCmd zcloudDepartmentUpdateCmd) {
        DepartmentUpdateCmd updateCmd = new DepartmentUpdateCmd();
        BeanUtils.copyProperties(zcloudDepartmentUpdateCmd, updateCmd);
        departmentService.edit(updateCmd);
        return SingleResponse.buildSuccess();
    }

    @Override
    public MultiResponse<ZcloudDepartmentInfoCo> getDeptList() {
        List<DepartmentCO> list = departmentService.getDeptList();
        List<ZcloudDepartmentInfoCo> zcloudUserCo = departmentCoConvertor.converCOsToDubboCOs(list);
        return MultiResponse.of(zcloudUserCo);
    }

    @Override
    public MultiResponse<ZcloudDepartmentInfoCo> listByDeptIds(Collection<Long> collection) {
        List<DepartmentCO> listed = departmentService.listByDepartIds(collection);
        List<ZcloudDepartmentInfoCo> zcloudPostCo = departmentCoConvertor.converCOsToDubboCOs(listed);
        return MultiResponse.of(zcloudPostCo);
    }

    @Override
    @Cacheable(value = "deptAllNameCache", key="'deptId:'+#deptId")
    public String getFullName(Long deptId) {
        List<DepartmentCO> list = departmentService.getDeptList();
        String name = Tools.getTreeFullName(list,"id", "parentId", "name", deptId);
        return name;
    }

    @Override
    public Map<Long, String> listFullName(List<Long> list) {
        List<DepartmentCO> deptlist = departmentService.getDeptList();
        Map<Long,  String> map = new HashMap<>();
        for (Long id : list){
            map.put(id, departmentService.getFullNameForList(id, deptlist));
        }
        return map;
    }

    @Override
    public SingleResponse<Long> addHumanUserDept(ZcloudDepartmentAddCmd zcloudDepartmentAddCmd) {
        return null;
    }
}
