package com.ssm.controller;

import com.ssm.core.bean.GenericResponse;
import com.ssm.core.util.SessionHelp;
import com.ssm.core.utils.BigDecimalUtils;
import com.ssm.manage.entity.CourseAccount;
import com.ssm.manage.query.CourseAccountRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmCourseAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javafx.scene.input.InputMethodTextRun;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Api(value="疗程账户controller",tags={"疗程账户相关接口"})
@RestController
@RequestMapping("/demay/ssm/couAccount")
@Slf4j
public class SsmCourseAccountController {
    @Autowired
    private SsmCourseAccountService ssmCourseAccountService;

    @ApiOperation(value = "疗程账户查询  (门店端-预约叫号-开单疗程账户查询)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/list")
    public GenericResponse list(@RequestBody CourseAccountRequest courseAccountRequest) {
        GenericResponse genericResponse = new GenericResponse();
//        if (null == courseAccountRequest.getStoreId()){
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "门店ID");
//            return genericResponse;
//        }
//        if (null == courseAccountRequest.getMemberId()){
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"会员ID");
//            return genericResponse;
//        }
        List<CourseAccount> list = new ArrayList<>();
        try {
            list = ssmCourseAccountService.selectListByMemberList(courseAccountRequest);
            for (CourseAccount courseAccount : list) {
                courseAccount.setPrice((BigDecimalUtils.div(courseAccount.getCourseStandardMoney(),BigDecimal.valueOf(courseAccount.getCourseStandardCount())).setScale(2)).multiply(BigDecimal.valueOf(1)));
                courseAccount.setCourseRestPrice(BigDecimalUtils.multiply(courseAccount.getPrice(),BigDecimal.valueOf(courseAccount.getCourseRestCount())));
                courseAccount.setCourseConsumePrice(BigDecimalUtils.multiply(courseAccount.getPrice(),BigDecimal.valueOf(courseAccount.getCourseConsumeCount())));
            }
        }catch (Exception e){
            log.error("SsmCourseAccountController.list:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "疗程退页面疗程账户查询  (门店端-疗程退-疗程退页面疗程账户查询)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/refundList")
    public GenericResponse refundList(@RequestBody CourseAccountRequest courseAccountRequest) {
        GenericResponse genericResponse = new GenericResponse();
        if (null == courseAccountRequest.getStoreId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "门店ID");
            return genericResponse;
        }
        if (null == courseAccountRequest.getMemberId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"会员ID");
            return genericResponse;
        }
        List<CourseAccount> list = new ArrayList<>();
        try {
            list = ssmCourseAccountService.selectRefundList(courseAccountRequest);
        }catch (Exception e){
            log.error("SsmCourseAccountController.refundList:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "单个疗程账户查询  (门店端-单个疗程账户查询)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/one")
    public GenericResponse one(@RequestBody CourseAccountRequest courseAccountRequest) {
        GenericResponse genericResponse = new GenericResponse();

        CourseAccount courseAccount = new CourseAccount();
        try {
            courseAccount = ssmCourseAccountService.selectByPrimaryKey(courseAccountRequest.getId());
        }catch (Exception e){
            log.error("SsmCourseAccountController.one:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setData(courseAccount);
        return genericResponse;
    }


    @ApiOperation(value = "疗程账户修改  (门店端-疗程单明细-疗程账户修改)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/update")
    public GenericResponse update(@RequestBody CourseAccountRequest courseAccountRequest) {
        GenericResponse genericResponse = new GenericResponse();
        if (null == courseAccountRequest.getId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "账户ID");
            return genericResponse;
        }
        String phone = courseAccountRequest.getStaffPhone();
        int i = 0;
        try {
            i = ssmCourseAccountService.update(courseAccountRequest, phone);
        }catch (Exception e){
            log.error("SsmCourseAccountController.update:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }
}
