package com.mortals.xhx.module.project.web;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.base.system.param.service.ParamService;
import com.mortals.xhx.base.system.user.model.UserQuery;
import com.mortals.xhx.base.system.user.service.UserService;
import com.mortals.xhx.common.code.StudyStatusEnum;
import com.mortals.xhx.common.code.UserType;
import com.mortals.xhx.module.project.model.ProjectStudyQuery;
import com.mortals.xhx.module.project.service.ProjectStudyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.mortals.framework.model.Context;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mortals.framework.web.BaseCRUDJsonBodyMappingController;
import com.mortals.xhx.module.project.model.ProjectEntity;
import com.mortals.xhx.module.project.service.ProjectService;
import org.apache.commons.lang3.ArrayUtils;
import com.mortals.framework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import static com.mortals.framework.ap.SysConstains.*;
/**
*
* 学习项目
*
* @author zxfei
* @date 2023-02-23
*/
@RestController
@RequestMapping("project")
public class ProjectController extends BaseCRUDJsonBodyMappingController<ProjectService,ProjectEntity,Long> {

    @Autowired
    private ParamService paramService;

    @Autowired
    private UserService userService;

    @Autowired
    private ProjectStudyService projectStudyService;

    public ProjectController(){
        super.setModuleDesc( "学习项目");
    }

    @Override
    protected void init(Map<String, Object> model, Context context) {
        this.addDict(model, "projectType", paramService.getParamBySecondOrganize("Project","projectType"));
        this.addDict(model, "top", paramService.getParamBySecondOrganize("Project","top"));
        this.addDict(model, "readRequest", paramService.getParamBySecondOrganize("Project","readRequest"));
        this.addDict(model, "createUserId", userService.find(new UserQuery()).stream().collect(Collectors.toMap(x->x.getId().toString(),y->y.getRealName())));
        this.addDict(model, "studyStatus", StudyStatusEnum.getEnumMap());
        super.init(model, context);
    }

    @Override
    protected void doListBefore(ProjectEntity query, Map<String, Object> model, Context context) throws AppException {
        List<OrderCol> orderColList = new ArrayList<>();
        orderColList.add(new OrderCol("createTime", OrderCol.DESCENDING));
        query.setOrderColList(orderColList);
        super.doListBefore(query, model, context);
    }

    @Override
    protected int editAfter(Long id, Map<String, Object> model, ProjectEntity entity, Context context) throws AppException {
       entity.setPerson(0);
        List<Long> personIds = projectStudyService.find(new ProjectStudyQuery().projectId(entity.getId())).stream().map(item -> item.getId()).collect(Collectors.toList());
        entity.setPersonIds(personIds);
        UserQuery userQuery = new UserQuery();
        userQuery.setUserType(UserType.WORK_PERSON.getValue());
        int count = userService.count(userQuery, context);
        if(count!=personIds.size()){
            entity.setPerson(1);
        }
        return super.editAfter(id, model, entity, context);
    }
}