package com.mortals.xhx.module.project.web;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.util.DateUtils;
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.module.project.model.ProjectEntity;
import com.mortals.xhx.module.project.service.ProjectService;
import org.checkerframework.checker.units.qual.A;
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.ProjectStudyEntity;
import com.mortals.xhx.module.project.service.ProjectStudyService;
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/study")
public class ProjectStudyController extends BaseCRUDJsonBodyMappingController<ProjectStudyService,ProjectStudyEntity,Long> {

    @Autowired
    private ParamService paramService;

    @Autowired
    private UserService userService;
    @Autowired
    private ProjectService projectService;

    public ProjectStudyController(){
        super.setModuleDesc( "项目学习人员");
    }

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

        super.init(model, context);
    }


    @Override
    protected void doListBefore(ProjectStudyEntity query, Map<String, Object> model, Context context) throws AppException {
        super.doListBefore(query, model, context);
        query.setUserId(this.service.getContextUserId(context));
        List<OrderCol> orderColList = new ArrayList<>();
        orderColList.add(new OrderCol("top", OrderCol.DESCENDING));
        orderColList.add(new OrderCol("studyStatus", OrderCol.ASCENDING));
        orderColList.add(new OrderCol("createTime", OrderCol.DESCENDING));
        query.setOrderColList(orderColList);
    }


    @Override
    protected int viewAfter(Long id, Map<String, Object> model, ProjectStudyEntity entity, Context context) throws AppException {
        ProjectEntity projectEntity = projectService.get(entity.getProjectId());
        entity.setStudyContent(projectEntity.getStudyContent());
        entity.setFileName(projectEntity.getFileName());
        entity.setFilePath(projectEntity.getFilePath());

        //判断时间是否在学习时间范围内
        Date projectStartTime = projectEntity.getProjectStartTime();
        Date projectEndTime = projectEntity.getProjectEndTime();
        entity.setProjectStartTime(projectEntity.getProjectStartTime());
        entity.setProjectEndTime(projectEntity.getProjectEndTime());

        long curTime = DateUtils.getCurrDatetime().getTime();
        if(projectEndTime.getTime()>curTime&&curTime >projectStartTime.getTime()){
            if(entity.getStudyStatus()==StudyStatusEnum.未学习.getValue()){
                entity.setStudyStatus(StudyStatusEnum.已学习.getValue());
                entity.setUpdateTime(new Date());
                entity.setUpdateUserId(this.service.getContextUserId(context));
                this.service.update(entity,context);
            }
        }
        return super.viewAfter(id, model, entity, context);
    }
}