package com.cm.central.control.service.projectleader.impl;

import com.cm.central.control.pojo.dtos.projectleader.ClientProjectLeaderDTO;
import com.cm.common.base.AbstractService;
import com.cm.common.exception.RemoveException;
import com.cm.common.exception.SaveException;
import com.cm.common.exception.SearchException;
import com.cm.common.pojo.ListPage;
import com.cm.common.result.SuccessResult;
import com.cm.common.result.SuccessResultData;
import com.cm.common.result.SuccessResultList;
import com.cm.common.utils.HashMapUtil;
import com.cm.common.utils.UUIDUtil;
import com.cm.central.control.dao.projectleader.IProjectLeaderDao;
import com.cm.central.control.pojo.dtos.projectleader.ProjectLeaderDTO;
import com.cm.central.control.pojo.vos.projectleader.ProjectLeaderVO;
import com.cm.central.control.service.projectleader.IProjectLeaderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName: ProjectLeaderServiceImpl
 * @Description: 项目负责人
 * @Author: WenG
 * @Date: 2020-07-30 19:04
 * @Version: 1.0
 **/
@Service
public class ProjectLeaderServiceImpl extends AbstractService implements IProjectLeaderService {

    @Autowired
    private IProjectLeaderDao projectLeaderDao;

    @Override
    public SuccessResult saveProjectLeader(ProjectLeaderVO projectLeaderVO) throws Exception {
        saveProjectLeaderInfo(null, projectLeaderVO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult saveProjectLeaderByToken(String token, ProjectLeaderVO projectLeaderVO) throws Exception {
        saveProjectLeaderInfo(token, projectLeaderVO);
        return new SuccessResult();
    }

    @Override
    public String saveProjectLeaderReturnId(ProjectLeaderVO projectLeaderVO) throws Exception {
        return saveProjectLeaderInfoReturnId(null, projectLeaderVO);
    }

    @Override
    public String saveProjectLeaderByTokenReturnId(String token, ProjectLeaderVO projectLeaderVO) throws Exception {
        return saveProjectLeaderInfoReturnId(token, projectLeaderVO);
    }

    /**
     * 新增项目负责人
     *
     * @param token
     * @param projectLeaderVO
     * @throws Exception
     */
    private void saveProjectLeaderInfo(String token, ProjectLeaderVO projectLeaderVO) throws Exception {
        saveProjectLeaderInfoReturnId(token, projectLeaderVO);
    }

    /**
     * 新增项目负责人
     *
     * @param token
     * @param projectLeaderVO
     * @return projectLeaderId
     * @throws Exception
     */
    private String saveProjectLeaderInfoReturnId(String token, ProjectLeaderVO projectLeaderVO) throws Exception {
        String projectLeaderId = UUIDUtil.getUUID();
        Map<String, Object> params = HashMapUtil.beanToMap(projectLeaderVO);
        params.put("projectLeaderId", projectLeaderId);
        if (token != null) {
            setSaveInfo(token, params);
        } else {
            setSaveInfo(params);
        }
        projectLeaderDao.saveProjectLeader(params);
        return projectLeaderId;
    }

    @Override
    public SuccessResult removeProjectLeader(String ids) throws RemoveException {
        removeProjectLeaderInfo(null, ids);
        return new SuccessResult();
    }

    @Override
    public SuccessResult removeProjectLeaderByToken(String token, String ids) throws RemoveException {
        removeProjectLeaderInfo(token, ids);
        return new SuccessResult();
    }

    /**
     * 删除项目负责人
     *
     * @param token
     * @param ids
     */
    private void removeProjectLeaderInfo(String token, String ids) {
        Map<String, Object> params = getHashMap(3);
        params.put("projectLeaderIds", Arrays.asList(ids.split("_")));
        if (token != null) {
            setUpdateInfo(token, params);
        } else {
            setUpdateInfo(params);
        }
        projectLeaderDao.removeProjectLeader(params);
    }

    @Override
    public void deleteProjectLeader(String ids) throws RemoveException {
        Map<String, Object> params = getHashMap(3);
        params.put("projectLeaderIds", Arrays.asList(ids.split("_")));
        projectLeaderDao.deleteProjectLeader(params);
    }

    @Override
    public SuccessResult updateProjectLeader(String projectLeaderId, ProjectLeaderVO projectLeaderVO) throws Exception {
        updateProjectLeaderInfo(null, projectLeaderId, projectLeaderVO);
        return new SuccessResult();
    }

    @Override
    public SuccessResult updateProjectLeaderByToken(String token, String projectLeaderId, ProjectLeaderVO projectLeaderVO) throws Exception {
        updateProjectLeaderInfo(token, projectLeaderId, projectLeaderVO);
        return new SuccessResult();
    }

    /**
     * 修改项目负责人
     *
     * @param token
     * @param projectLeaderId
     * @param projectLeaderVO
     */
    private void updateProjectLeaderInfo(String token, String projectLeaderId, ProjectLeaderVO projectLeaderVO) throws Exception {
        Map<String, Object> params = HashMapUtil.beanToMap(projectLeaderVO);
        params.put("projectLeaderId", projectLeaderId);
        if (token != null) {
            setUpdateInfo(token, params);
        } else {
            setUpdateInfo(params);
        }
        projectLeaderDao.updateProjectLeader(params);
    }

    @Override
    public ProjectLeaderDTO getProjectLeaderById(String projectLeaderId) throws SearchException {
        Map<String, Object> params = super.getHashMap(1);
        params.put("projectLeaderId", projectLeaderId);
        return projectLeaderDao.getProjectLeader(params);
    }

    @Override
    public List<ProjectLeaderDTO> listProjectLeader(Map<String, Object> params) throws SearchException {
        return projectLeaderDao.listProjectLeader(params);
    }

    @Override
    public SuccessResultList<List<ProjectLeaderDTO>> listPageProjectLeader(ListPage page) throws SearchException {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<ProjectLeaderDTO> projectLeaderDTOs = projectLeaderDao.listProjectLeader(page.getParams());
        PageInfo<ProjectLeaderDTO> pageInfo = new PageInfo<>(projectLeaderDTOs);
        return new SuccessResultList<>(projectLeaderDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

    @Override
    public Integer countNumberProjectLeader(Map<String, Object> params) throws SearchException {
        Integer count = projectLeaderDao.countProjectLeader(params);
        return count == null ? 0 : count;
    }

    @Override
    public SuccessResultData<Integer> countProjectLeader(Map<String, Object> params) throws SearchException {
        return new SuccessResultData<>(countNumberProjectLeader(params));
    }

    @Override
    public ClientProjectLeaderDTO getProjectLeaderByClientId(String clientId) throws SearchException {
        return projectLeaderDao.getProjectLeaderByClientId(clientId);
    }

}