package com.rf.psychological.scale.rest;

import com.rf.psychological.base.rest.BaseController;
import com.rf.psychological.dao.dto.CognitiveTaskEntityDto;
import com.rf.psychological.dao.model.CognitiveTaskEntity;
import com.rf.psychological.group.dao.model.GroupEntity;
import com.rf.psychological.group.service.GroupInfoService;
import com.rf.psychological.scale.dao.model.SubjectEntity;
import com.rf.psychological.scale.service.CognitiveTaskService;
import com.rf.psychological.scale.service.SubjectService;
import com.rf.psychological.security.SafetyProcess;
import com.rf.psychological.scale.service.CategoryService;
import com.rf.psychological.utils.Constant;
import com.rf.psychological.utils.Result;
import com.rf.psychological.scale.dao.model.CategoryEntity;
import com.rf.psychological.wxpay.model.PromotionInfo;
import com.rf.psychological.wxpay.service.PromotionInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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


/**
 * @author zsy
 * @description:量表类别信息管理
 * @date 2021/6/17 15:55
 */

@Slf4j
@RestController
@RequestMapping("/category")
@Api(tags = "量表类别信息管理")
public class CategoryController extends BaseController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private GroupInfoService groupInfoService;

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private CognitiveTaskService cognitiveTaskService;

    @Autowired
    private PromotionInfoService promotionInfoService;

    /**
     * 添加量表类别信息
     *
     * @param categoryEntity
     * @return
     */
    @SafetyProcess
    @PostMapping("/save")
    @ApiOperation(value = "量表/认知任务类别信息--保存",notes = "新增时id为空，更新时id必传")
    public Result saveCategoryInfo(CategoryEntity categoryEntity) {

        if (categoryEntity.getName() == null || categoryEntity.getName().trim().length() == 0) {
            return failBadRequest(null, "类别名称为空");
        }
        if (categoryEntity.getEname() == null || categoryEntity.getEname().trim().length() <= 0) {
            return failBadRequest(null, "类别英文名为空");
        }
        if (categoryEntity.getSequence() == null || categoryEntity.getSequence().trim().length() <= 0) {
            return failBadRequest(null, "排序为空");
        }
        List<CategoryEntity> categoryEntityList = this.categoryService.getCategoryByName(categoryEntity.getName(), categoryEntity.getEname(), categoryEntity.getSequence());
        if (categoryEntityList.size() > 0) {
            return failBadRequest(null, "类别已存在");
        }
        //更新
        if(StringUtils.isNotBlank(categoryEntity.getId())){
            CategoryEntity entity = this.categoryService.findById(categoryEntity.getId());
            if(entity == null){
                return fail("类别不存在");
            }
            entity.setEname(categoryEntity.getEname());
            entity.setName(categoryEntity.getName());
            entity.setSequence(categoryEntity.getSequence());
            entity.setType(categoryEntity.getType());
            this.categoryService.save(entity);
        }else{
            //新增
            this.categoryService.save(categoryEntity);
        }

        return success();
    }

    /**
     * 量表查询所有类别信息
     *
     * @return
     */
    @SafetyProcess
    @GetMapping("/getCategoryList")
    @ApiOperation(value = "量表查询所有类别信息")
    public Result getCategoryList(String institutionNo, String gId) {
        //游客用户无gid，数据库配置特定用户
        if (StringUtils.isEmpty(gId)){
            GroupEntity group = groupInfoService.findGroupByInstitutionNoAndName(institutionNo, Constant.DEFAULT_GROUP_NAME);
            if (group != null){
                gId = group.getId();
            }
        }
        return success(this.categoryService.findALLCategorySubject(institutionNo, gId));
    }

    /**
     * 认知任务查询所有类别信息
     *
     * @return
     */
    @SafetyProcess
    @GetMapping("/getCognizeCategoryList")
    @ApiOperation(value = "认知任务查询所有类别信息")
    public Result getCognizeCategoryList(String institutionNo, String gId) {
        //游客用户无id，数据库配置特定用户
        if (StringUtils.isEmpty(gId)){
            GroupEntity group = groupInfoService.findGroupByInstitutionNoAndName(institutionNo, Constant.DEFAULT_GROUP_NAME);
            if (group != null){
                gId = group.getId();
            }
        }
        return success(this.categoryService.findAllCT(institutionNo, gId));
    }

    /**
     * 全量查询所有类别信息-不加密，后台维护管理使用
     */
    @GetMapping("/loadCategory")
    public Result<List<CategoryEntity>> loadCategory() {
        List<CategoryEntity> categoryEntities = this.categoryService.findAlls();
        return success(categoryEntities);
    }

    /**
     * 相关测评
     */
    @SafetyProcess
    @GetMapping("/getRelevantEvaluation")
    @ApiOperation(value = "获取相关测评信息",notes = " flag:量表或认知任务flag,type:类型0量表1认知任务")
    public Result getRelevantEvaluation(String flag, String type) {
        if (type.equals(Constant.DEFAULT_VALUE_ZERO)){
            List<SubjectEntity> subjectEntities = this.subjectService.getRelevantEvaluation(flag);
            return success(subjectEntities);
        }else if (type.equals(Constant.DEFAULT_VALUE_ONE)){
            List<CognitiveTaskEntity> cognitiveTaskEntities = this.cognitiveTaskService.getRelevantEvaluation(flag);
            List<CognitiveTaskEntityDto> dto2List = new ArrayList<>();
            if(cognitiveTaskEntities.size()>0){
                cognitiveTaskEntities.forEach(cognitiveTaskEntity -> {
                    CognitiveTaskEntityDto dto = new CognitiveTaskEntityDto(cognitiveTaskEntity);
                    PromotionInfo infoBySubjectId = promotionInfoService.findInfoBySubjectId(cognitiveTaskEntity.getId());
                    dto.setPrice( infoBySubjectId ==null?0.0:infoBySubjectId.getPrice());
                    dto2List.add(dto);
                });
            }
            return success(dto2List);
        }else {
            return fail("类型参数错误！");
        }
    }

}
