package io.hmit.modules.fhjw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.hmit.common.exception.ErrorCode;
import io.hmit.common.exception.HmitException;
import io.hmit.common.page.PageData;
import io.hmit.common.service.impl.CrudServiceImpl;
import io.hmit.common.utils.ConvertUtils;
import io.hmit.modules.fhjw.dao.FhjwCaseDao;
import io.hmit.modules.fhjw.dto.FhjwCaseDTO;
import io.hmit.modules.fhjw.dto.UserDTO;
import io.hmit.modules.fhjw.entity.FhjwCaseEntity;
import io.hmit.modules.fhjw.entity.FhjwFeedbackEntity;
import io.hmit.modules.fhjw.entity.UserEntity;
import io.hmit.modules.fhjw.service.FhjwCaseService;
import io.hmit.modules.fhjw.service.FhjwCommentService;
import io.hmit.modules.fhjw.service.FhjwFeedbackService;
import io.hmit.modules.fhjw.service.RoleService;
import io.hmit.modules.fhjw.service.UserRoleService;
import io.hmit.modules.fhjw.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 案件表
 *
 * @author zsh 408538940@qq.com
 * @since 1.0.0 2021-06-02
 */
@Service
public class FhjwCaseServiceImpl extends CrudServiceImpl<FhjwCaseDao, FhjwCaseEntity, FhjwCaseDTO> implements FhjwCaseService {

    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private FhjwCommentService fhjwCommentService;

    @Autowired
    private FhjwFeedbackService fhjwFeedbackService;


    @Override
    public QueryWrapper<FhjwCaseEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String caseNum = (String)params.get("caseNum");
        String caseName = (String)params.get("caseName");
        String flag = (String)params.get("flag");

        QueryWrapper<FhjwCaseEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.like(StringUtils.isNotBlank(caseNum), "case_num", caseNum);
        wrapper.like(StringUtils.isNotBlank(caseName), "case_name", caseName);
        wrapper.eq(StringUtils.isNotBlank(flag), "flag", flag);

        return wrapper;
    }


    @Override
    public FhjwCaseDTO assembleUserList(Long caseId) {
        FhjwCaseDTO data = get(caseId);
        Optional<FhjwCaseDTO> optional = Optional.ofNullable(data);
        optional.ifPresent(f -> {
            Map<String, Object> params = new HashMap<>();
            params.put("caseId",f.getId());
            List<UserDTO> userLists = userService.dtoAddUserRole(params);
            f.setUserLists(userLists);
        });

        return data;
    }

    @Override
    public FhjwCaseDTO getByCaseNum(String caseNum,String peopleName) {
        FhjwCaseEntity entity = baseDao.selectOne(new QueryWrapper<FhjwCaseEntity>()
                .eq("case_num",caseNum).eq("people_name",peopleName));
        return ConvertUtils.sourceToTarget(entity,FhjwCaseDTO.class);
    }


    @Override
    public PageData<FhjwCaseDTO> page(Map<String, Object> params){

        IPage<FhjwCaseEntity> page = super.page2(params);
        //在返回的page中增加 用户名字 字段
        page.getRecords().stream().forEach(item ->{
            Optional<UserEntity> users = Optional.ofNullable(userService.selectById(item.getCreator()));
            users.ifPresent( u -> item.setUserName(u.getUsername()));
        });

        return getPageData(page, currentDtoClass());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long[] ids) {
        String result = "";
        for (long id:ids) {
            List<Long> userids= fhjwCommentService.getUserIdByCase(id);
            String caseNum =get(id).getCaseNum();
            List<FhjwFeedbackEntity> list= fhjwFeedbackService.getByCaseNum(caseNum);
            if(userids.size()>0 || list.size()>0){
                throw new HmitException("此案件存在相关评价及反馈，请先删除对应的评价与反馈信息！");
            }
            deleteById(id);
        }
    }

    @Override
    public void save(FhjwCaseDTO dto) {

        super.save(dto);
    }
}
