package com.rf.psychological.wxpay.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.rf.psychological.base.rest.BaseController;
import com.rf.psychological.dao.model.CognitiveTaskEntity;
import com.rf.psychological.scale.dao.model.ScaleDetailsEntity;
import com.rf.psychological.scale.dao.model.SubjectEntity;
import com.rf.psychological.scale.dao.repository.CognitiveTaskRepository;
import com.rf.psychological.scale.service.ScaleDetailsService;
import com.rf.psychological.scale.service.SubjectService;
import com.rf.psychological.security.SafetyProcess;
import com.rf.psychological.utils.Result;
import com.rf.psychological.wxpay.model.PromotionInfo;
import com.rf.psychological.wxpay.service.OrderInfoService;
import com.rf.psychological.wxpay.service.PromotionInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Description:量表收费管理
 * @Author: zsf
 * @Date: 2022/6/29
 */
@RestController
@RequestMapping("/api/promotionInfo")
@Api(tags = "量表收费管理")
@Slf4j
@ConditionalOnProperty(prefix="wx.pay",name = "is_lan", havingValue = "false")
public class PromotionInfoController extends BaseController {
    @Autowired
    private PromotionInfoService infoService;

    @Autowired
    private CognitiveTaskRepository repository;

    @Autowired
    private SubjectService subjectService;


    @Autowired
    private ScaleDetailsService scaleDetailsService;

    @Autowired
    private OrderInfoService orderInfoService;


    @SafetyProcess
    @GetMapping("/queryPromotionBySubjectId/{subjectId}")
    @ApiOperation(value = "根据量表id获取到收费价格")
    public Result queryPromotionBySubjectId(@PathVariable String subjectId){
        try {
            PromotionInfo info = infoService.findInfoByFlag(subjectId);
            ScaleDetailsEntity entity = scaleDetailsService.getSubjectDetailsByFlag(info.getFlag());
            if (entity != null){
                info.setName(entity.getScaleName());
            }
            return success(info);
        }catch (Exception e ){
            e.printStackTrace();
            return fail();
        }
    }

    @SafetyProcess
    @GetMapping("/queryPromotionDetail/{subjectId}")
    @ApiOperation(value = "获取促销详情")
    public Result queryPromotionDetail(@PathVariable String subjectId){
        try {
            PromotionInfo info = infoService.findInfoByFlag(subjectId);
            if (info == null){
                return success(null,"免费");
            }
            int count = orderInfoService.countNum(info.getFlag(),DateUtil.format(info.getPromotionStartTime(), DatePattern.NORM_DATETIME_PATTERN));
            info.setResidueNum(info.getPromotionNum()-count);
            return success(info);
        }catch (Exception e ){
            e.printStackTrace();
            return fail();
        }
    }
    @GetMapping("/save")
    public Result saveTask(){
        List<CognitiveTaskEntity> list =  repository.findAll();
        for (CognitiveTaskEntity taskEntity : list){
            PromotionInfo promotionInfo = new PromotionInfo();
            promotionInfo.setCreateTime(DateUtil.now());
            promotionInfo.setUpdateTime(DateUtil.now());
            promotionInfo.setName(taskEntity.getName());
            promotionInfo.setPrice(0.01);
            promotionInfo.setType("1");
            promotionInfo.setSubjectId(taskEntity.getId());
            infoService.saveOrUpdateInfo(promotionInfo);
        }
        return success();
    }

    @GetMapping("/updateFlag")
    public Result updateFlag(){
        //获取到所有量表
        List<SubjectEntity> subjectEntityList = subjectService.findAll("0");
        List<CognitiveTaskEntity> infos = repository.findAll();
        List<PromotionInfo> promotionInfos = infoService.findAll();
        promotionInfos.forEach(item->{
            for (SubjectEntity entity:subjectEntityList){
                if (item.getSubjectId().equals(entity.getId())){
                    item.setFlag(entity.getFlag());
                }
            }
        });
        promotionInfos.forEach(item->{
            for (CognitiveTaskEntity entity:infos){
                if (item.getSubjectId().equals(entity.getId())){
                    item.setFlag(entity.getTaskCode());
                }
            }
        });

        promotionInfos.forEach(item->{
            infoService.saveOrUpdateInfo(item);
        });

        return success();
    }


}
