package ink.wgink.service.department.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ink.wgink.common.base.DefaultBaseService;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.pojo.ListPage;
import ink.wgink.pojo.dtos.user.UserDTO;
import ink.wgink.pojo.result.SuccessResultList;
import ink.wgink.service.department.dao.IDepartmentAdjustmentDao;
import ink.wgink.service.department.enmus.DepartmentAdjustmentTypeEnum;
import ink.wgink.service.department.pojo.dtos.DepartmentAdjustmentDTO;
import ink.wgink.service.department.service.IDepartmentAdjustmentService;
import ink.wgink.service.department.service.IDepartmentUserAdjustmentService;
import ink.wgink.service.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: DepartmentAdjustmentServiceImpl
 * @Description: 组织部门日志
 * @Author: wanggeng
 * @Date: 2021/1/28 10:23 下午
 * @Version: 1.0
 */
@Service
public class DepartmentAdjustmentServiceImpl extends DefaultBaseService implements IDepartmentAdjustmentService {

    @Autowired
    private IDepartmentAdjustmentDao departmentAdjustmentDao;
    @Autowired
    private IUserService userService;
    @Autowired
    private IDepartmentUserAdjustmentService departmentUserAdjustmentService;

    @Override
    public void save(String oldDepartmentId, String oldDepartmentName, String newDepartmentId, String newDepartmentName, DepartmentAdjustmentTypeEnum departmentAdjustmentTypeEnum) {
        Map<String, Object> params = getHashMap(3);
        params.put("oldDepartmentId", oldDepartmentId);
        params.put("oldDepartmentName", oldDepartmentName);
        params.put("newDepartmentId", newDepartmentId);
        params.put("newDepartmentName", newDepartmentName);
        params.put("adjustmentType", departmentAdjustmentTypeEnum.getValue());
        params.put("creatorName", securityComponent.getCurrentUser().getUserName());
        setSaveInfo(params);
        departmentAdjustmentDao.save(params);
    }

    @Override
    public void save(List<String> userIds, String departmentId, String departmentName, String userDepartmentType) {
        if (userIds.isEmpty()) {
            return;
        }
        List<UserDTO> userDTOs = userService.listByUserIds(userIds);
        Map<String, Object> params = new HashMap<>(16);
        if (securityComponent.getCurrentUser() != null) {
            params.put("creatorName", securityComponent.getCurrentUser().getUserName());
        } else {
            params.put("creatorName", ISystemConstant.ADMIN);
        }
        setSaveInfo(params);
        for (UserDTO userDTO : userDTOs) {
            params.put("userId", userDTO.getUserId());
            params.put("userName", userDTO.getUserName());
            params.put("departmentId", departmentId);
            params.put("departmentName", departmentName);
            params.put("adjustmentType", userDepartmentType);
            departmentUserAdjustmentService.save(params);
        }
    }

    @Override
    public SuccessResultList<List<DepartmentAdjustmentDTO>> listPage(ListPage page) {
        PageHelper.startPage(page.getPage(), page.getRows());
        List<DepartmentAdjustmentDTO> departmentLogDTOs = departmentAdjustmentDao.list(page.getParams());
        PageInfo<DepartmentAdjustmentDTO> pageInfo = new PageInfo<>(departmentLogDTOs);
        return new SuccessResultList<>(departmentLogDTOs, pageInfo.getPageNum(), pageInfo.getTotal());
    }

}
