package com.ssm.controller;

import com.ssm.core.bean.GenericResponse;
import com.ssm.core.util.SessionHelp;
import com.ssm.manage.entity.RechargeAccount;
import com.ssm.manage.query.RechargeAccountRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmRechargeAccountService;
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.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.util.ArrayList;
import java.util.List;

@Api(value="充值账户controller",tags={"充值账户相关接口"})
@RestController
@RequestMapping("/demay/ssm/recAccount")
@Slf4j
public class SsmRechargeAccountController {
    @Autowired
    private SsmRechargeAccountService ssmRechargeAccountService;

    @ApiOperation(value = "查询门店用户所拥有的全部充值账户",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/list")
    public GenericResponse selectList(@RequestBody RechargeAccountRequest rechargeAccountRequest){
        GenericResponse<RechargeAccount> genericResponse = new GenericResponse<>();
//        if (null == rechargeAccountRequest.getStoreId()){
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "门店ID");
//            return genericResponse;
//        }
        if (null == rechargeAccountRequest.getMemberId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"会员ID");
            return genericResponse;
        }
        List<RechargeAccount> list = new ArrayList<>();
        try {
            list = ssmRechargeAccountService.selectList(rechargeAccountRequest);
        }catch (Exception e){
            log.error("SsmRechargeAccountController.selectList:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "收银查询门店用户所拥有的全部充值账户",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/cashList")
    public GenericResponse cashList(@RequestBody RechargeAccountRequest rechargeAccountRequest){
        GenericResponse<RechargeAccount> genericResponse = new GenericResponse<>();
        if (null == rechargeAccountRequest.getStoreId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "门店ID");
            return genericResponse;
        }
        if (null == rechargeAccountRequest.getMemberId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"会员ID");
            return genericResponse;
        }
        List<RechargeAccount> list = new ArrayList<>();
        try {
            list = ssmRechargeAccountService.cashSelectList(rechargeAccountRequest);
        }catch (Exception e){
            log.error("SsmRechargeAccountController.cashList:",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 selectOne(@RequestBody RechargeAccountRequest rechargeAccountRequest){
        GenericResponse<RechargeAccount> genericResponse = new GenericResponse<>();
        if (null == rechargeAccountRequest.getStoreId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "门店ID");
            return genericResponse;
        }
        if (null == rechargeAccountRequest.getMemberId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"会员ID");
            return genericResponse;
        }
        if (null == rechargeAccountRequest.getRechargeTypeId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"充值类别ID");
            return genericResponse;
        }
        RechargeAccount rechargeAccount = new RechargeAccount();
        try {
            rechargeAccount = ssmRechargeAccountService.selectOne(rechargeAccountRequest);
        }catch (Exception e){
            log.error("SsmRechargeAccountController.selectOne:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (null == rechargeAccount){
            genericResponse.setCodeAndMsg(ResponseCode.DATA_DOES_NOT_EXISTS_S,"充值账户");
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setData(rechargeAccount);
        return genericResponse;
    }

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

    @ApiOperation(value = "通过ID查询门店用户所拥有的一个充值账户  (门店端-充值单-通过ID查询所选择充值类别对应会员的充值账户)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/selectById")
    public GenericResponse selectById(@RequestBody RechargeAccountRequest rechargeAccountRequest){
        GenericResponse<RechargeAccount> genericResponse = new GenericResponse<>();
        RechargeAccount rechargeAccount = new RechargeAccount();
        try {
            rechargeAccount = ssmRechargeAccountService.selectByPrimaryKey(rechargeAccountRequest.getId());
        }catch (Exception e){
            log.error("SsmRechargeAccountController.selectById:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (null == rechargeAccount){
            genericResponse.setCodeAndMsg(ResponseCode.DATA_DOES_NOT_EXISTS_S,"充值账户");
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setData(rechargeAccount);
        return genericResponse;
    }
}
