package com.ssm.controller;


import com.github.pagehelper.PageInfo;
import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.response.PageResponse;
import com.ssm.manage.utils.SessionHelp;
import com.ssm.manage.entity.RechargeAccountGive;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.*;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.StaffResponse;
import com.ssm.manage.service.SsmStoreAccountOutinService;
import com.ssm.util.RedisClient;
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.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.ssm.manage.utils.Constant.staffKey;

@Api(value="充值促销仅门店使用卡转店controller",tags={"充值促销仅门店使用转店相关接口"})
@RestController
@RequestMapping("/demay/ssm/storeAccountOutin")
@Slf4j
public class SsmStoreAccountOutinController {

    @Autowired
    private RedisClient<StaffResponse> redisClient;
    @Autowired
    private SsmStoreAccountOutinService ssmStoreAccountOutinService;

    @ApiOperation(value = "促销仅门店使用卡转店申请查询", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public PageResponse page(@RequestBody RechargeAccountOutinRequest rechargeAccountOutinRequest, HttpSession session) {
        PageResponse<RechargeAccountOutinRequest> genericResponse = new PageResponse();
        PageInfo<RechargeAccountOutinRequest> pageInfo = new PageInfo<>();
        try {

            pageInfo = ssmStoreAccountOutinService.selectPage(rechargeAccountOutinRequest);
        }catch (SmsException e){
            log.error("SsmStoreAccountOutinController page",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e) {
            log.error("SsmStoreAccountOutinController.page:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setPageInfo(pageInfo);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "查询用户促销仅门店使用赠送卡账户(查询未过期)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/accounGivetList")
    public GenericResponse accounGivetList(@RequestBody RechargeAccountRequest rechargeAccountRequest, HttpSession session){
        GenericResponse<RechargeAccountGive> genericResponse = new GenericResponse<>();
        List<RechargeAccountGive> List=new ArrayList<>();
//        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;
        }

        try {
            List = ssmStoreAccountOutinService.selectGiveAccount(rechargeAccountRequest);
        }catch (SmsException e){
            log.error("SsmStoreAccountOutinController.selectGiveAccount:",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmStoreAccountOutinController.selectGiveAccount:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(List);
        return genericResponse;
    }

    @ApiOperation(value = "添加活动赠卡转门店申请", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public GenericResponse add(@RequestBody List<RechargeAccountOutinRequest> requestList, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        List<RechargeAccountOutinRequest> collect = requestList.stream().filter(rechargeAccountOutinRequest -> rechargeAccountOutinRequest.getOutStoreId() != null && rechargeAccountOutinRequest.getInStoreId() != null).collect(Collectors.toList());
        if(collect.size()!=requestList.size()){
            genericResponse.setCodeAndMsg(ResponseCode.MSG,"请选择转入或转出的门店");
            return genericResponse;
        }
        String staffPhone = SessionHelp.getLoginAccount(session);//"18070793360";
        for (RechargeAccountOutinRequest rechargeAccountOutinRequest : requestList) {
            rechargeAccountOutinRequest.setInputBy(staffPhone);
        }
        int i = 0;
        try {
            i = ssmStoreAccountOutinService.add(requestList);
        }catch (SmsException e){
            log.error("SsmStoreAccountOutinController add",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStoreAccountOutinController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    public String getStaffKey(String phone) {
        return staffKey + phone;
    }
    @ApiOperation(value = "确认（审核）活动赠卡转门店", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/updateState", method = RequestMethod.POST)
    public GenericResponse updateState(@RequestBody List<RechargeAccountOutinRequest> requestList, HttpSession session) {
        String loginAccount = SessionHelp.getLoginAccount(session);
        Long inputId =0L;
        String inputName = "";
        //设置录单人信息
        if (redisClient.hasKey(getStaffKey(loginAccount))) {
            StaffResponse staffResponse = redisClient.get(getStaffKey(loginAccount));
            inputId = staffResponse.getId();
            inputName = staffResponse.getStaffName();
        }
        for (RechargeAccountOutinRequest rechargeAccountOutinRequest : requestList) {
            rechargeAccountOutinRequest.setAuditName(inputName);
            rechargeAccountOutinRequest.setAuditId(inputId);
            rechargeAccountOutinRequest.setAuditDate(new Date());
            rechargeAccountOutinRequest.setInputBy(inputName);
        }
        GenericResponse genericResponse = new GenericResponse();
        List<RechargeAccountOutinRequest> collect = requestList.stream().filter(item -> item.getState() != 1).collect(Collectors.toList());
        if(collect==null || collect.size()==0){
            genericResponse.setCodeAndMsg(ResponseCode.DATA_NOT_ALLOW_UPDATES,"已审核，请勿重复");
            return genericResponse;
        }
        int i = 0;
        try {
            i = ssmStoreAccountOutinService.update(requestList);
        }catch (SmsException e){
            log.error("SsmStoreAccountOutinController update",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStoreAccountOutinController.update:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }


}
