package org.springblade.modules.app.manager;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wn.common.datamodel.ModelConvertUtil;
import com.wn.common.validator.Validator;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.app.common.dto.ProblemBackDTO;
import org.springblade.modules.app.common.dto.ProblemBackInsertDTO;
import org.springblade.modules.app.common.dto.UserDTO;
import org.springblade.modules.app.common.enums.ProblemBackStateEnum;
import org.springblade.modules.app.common.query.ProblemBackQuery;
import org.springblade.modules.app.constant.ErrorCode;
import org.springblade.modules.app.entity.ProblemBackDO;
import org.springblade.modules.app.service.IProblemBackService;
import org.springblade.modules.app.service.IAppUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class ProblemBackManager {

    @Autowired
    private IProblemBackService problemBackService;
    @Autowired
    private IAppUserService userService;

    public Boolean insert(ProblemBackInsertDTO dto, UserDTO userDTO) {
        Validator.notNull(dto, ErrorCode.INPUT_PARAMETERS_NULL);
        Validator.notEmpty(dto.getContent(), ErrorCode.INPUT_PARAMETERS_NULL);
        Validator.isTrue(dto.getContent().length() <= 500, ErrorCode.PROBLEM_BACK_CONTENT_TOOLONG);
        ProblemBackDTO problemBackDTO = ModelConvertUtil.copyTo(dto, ProblemBackDTO.class);
        problemBackDTO.setUserId(userDTO.getId());
        Long id = problemBackService.insert(problemBackDTO);
        Validator.notNull(id, ErrorCode.PROBLEM_BACK_INSERT_FAIL);
        return true;
    }

    public Boolean update(ProblemBackDTO dto) {
        problemBackService.update(Wrappers.<ProblemBackDO>lambdaUpdate()
                .eq(ProblemBackDO::getId, dto.getId())
                .set(ProblemBackDO::getReplyType, 1)
                .set(ProblemBackDO::getReplyContext, dto.getReplyContext()));
        return true;
    }

    public Page<ProblemBackDTO> page(ProblemBackQuery query) {
        if (!StringUtil.isEmpty(query.getUserId())) {
            List<Long> ids = new ArrayList<>();
            ids.add(query.getUserId());
            Page<ProblemBackDTO> page = problemBackService.page(query, ids);
            List<ProblemBackDTO> list = page.getRecords();
            if (CollectionUtils.isNotEmpty(list)) {
                for (ProblemBackDTO problemBackDTO : list) {
                    UserDTO userDTO = userService.getUserById(problemBackDTO.getUserId());
                    problemBackDTO.setUserName(StringUtil.isEmpty(userDTO) ? "" : userDTO.getUserName());
                }
            }
            page.setRecords(list);
            return page;
        }
        //查询用户ids,根据填写名称模糊匹配用户，得到用户id集合ids
        List<Long> ids = new ArrayList<>();
        if (query != null && StringUtils.isNotBlank(query.getUserName())) {
            List<UserDTO> list = userService.listByUserName(query.getUserName());
            if (CollectionUtils.isNotEmpty(list)) {
                for (UserDTO userDTO : list) {
                    ids.add(userDTO.getId());
                }
            }
        }
        Page<ProblemBackDTO> page = problemBackService.page(query, ids);
        List<ProblemBackDTO> list = page.getRecords();
        if (CollectionUtils.isNotEmpty(list)) {
            for (ProblemBackDTO problemBackDTO : list) {
                UserDTO userDTO = userService.getUserById(problemBackDTO.getUserId());
                problemBackDTO.setUserName(StringUtil.isEmpty(userDTO) ? "" : userDTO.getUserName());
            }
        }
        page.setRecords(list);
        return page;
    }

    public ProblemBackDTO info(Long id) {
        ProblemBackDTO problemBackDTO = problemBackService.infoById(id);
        Validator.notNull(problemBackDTO, ErrorCode.PROBLEM_BACK_ISNULL);
        UserDTO userDTO = userService.getUserById(problemBackDTO.getUserId());
        problemBackDTO.setUserName(StringUtil.isEmpty(userDTO) ? "" : userDTO.getUserName());
        //修改状态为已查看
        if (ProblemBackStateEnum.NOT_VIEWED.getCode() == problemBackDTO.getState().intValue()) {
            problemBackDTO.setState(ProblemBackStateEnum.VIEWED.getCode());
            problemBackDTO.setModifiedTime(new Date());
            problemBackService.update(problemBackDTO);
        }
        return problemBackDTO;
    }

}
