package com.ssm.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.github.pagehelper.PageInfo;
import com.ssm.core.bean.GenericResponse;

import com.ssm.core.bean.PageResponse;
import com.ssm.core.util.SessionHelp;
import com.ssm.core.utils.BigDecimalUtils;
import com.ssm.manage.entity.RechargeType;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.excelEntity.ExcelRechargeType;
import com.ssm.manage.query.RechargeTypeRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmRechargeTypeSevice;
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.BeanUtils;
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 org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

@Api(value="充值类别controller",tags={"充值类别相关接口"})
@RestController
@RequestMapping("/demay/ssm/recType")
@Slf4j
public class SsmRechargeTypeController {
    @Autowired
    private SsmRechargeTypeSevice rechargeTypeSevice;

    @ApiOperation(value = "分页查询充值类别  门店端-充值类别-分页查询充值类别",httpMethod = "POST",response = PageResponse.class)
    @RequestMapping("/page")
    public PageResponse selectPage(@RequestBody RechargeTypeRequest rechargeTypeRequest, HttpSession session){
        PageResponse<RechargeType> pageResponse = new PageResponse<>();
        PageInfo<RechargeType> pageInfo = new PageInfo<>();
//        rechargeTypeRequest.setStoreId(SessionHelp.getStoreId(session));
        try {
            pageInfo = rechargeTypeSevice.selectPage(rechargeTypeRequest);
        }catch (Exception e){
            log.error("SsmRechargeTypeController.selectPage",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    @ApiOperation(value = "充值类别添加  门店端-充值类别-充值类别添加",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/add")
    public GenericResponse add(@RequestBody RechargeTypeRequest rechargeTypeRequest){
        GenericResponse<RechargeType> genericResponse = new GenericResponse<>();
//        if (StringUtils.isBlank(rechargeTypeRequest.getRechargeTypeNum())){
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"充值类别编码");
//            return genericResponse;
//        }
        if (StringUtils.isBlank(rechargeTypeRequest.getRechargeTypeName())){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"充值类别名称");
            return genericResponse;
        }
        if (BigDecimalUtils.compareTo(rechargeTypeRequest.getTypeRatio(), rechargeTypeRequest.getRechargeMoney()) != -1){
            genericResponse.setCodeAndMsg(ResponseCode.NOT_MEET_REQUIREMENTS_S,"充值类别提成");
            return genericResponse;
        }
//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
//        rechargeTypeRequest.setStoreId(storeId);
//        rechargeTypeRequest.setStoreName(storeName);
        String phone = rechargeTypeRequest.getStaffPhone();
        int i = 0;
        try {
            if(rechargeTypeRequest.getDiscount().longValue()<0||rechargeTypeRequest.getDiscountFreeAmount().longValue()<0||rechargeTypeRequest.getGiftMoney().longValue()<0||rechargeTypeRequest.getTypeRatio().longValue()<0||rechargeTypeRequest.getValidTime().longValue()<0){
                throw new SmsException("010000","操作失败，不能输入负数");
            }
            i = rechargeTypeSevice.addRecharge(rechargeTypeRequest, phone);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmRechargeTypeController.add",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "充值类别修改   门店端-充值类别-充值类别修改",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/update")
    public GenericResponse update(@RequestBody RechargeTypeRequest rechargeTypeRequest){
        GenericResponse<RechargeType> genericResponse = new GenericResponse<>();
        if (BigDecimalUtils.compareTo(rechargeTypeRequest.getTypeRatio(), rechargeTypeRequest.getRechargeMoney()) != -1){
            genericResponse.setCodeAndMsg(ResponseCode.NOT_MEET_REQUIREMENTS_S,"充值类别提成");
            return genericResponse;
        }
//        rechargeTypeRequest.setStoreId(SessionHelp.getStoreId(session));
        String phone = rechargeTypeRequest.getStaffPhone();
        int i = 0;
        try {
            if(rechargeTypeRequest.getDiscount().longValue()<0||rechargeTypeRequest.getDiscountFreeAmount().longValue()<0||rechargeTypeRequest.getGiftMoney().longValue()<0||rechargeTypeRequest.getTypeRatio().longValue()<0||rechargeTypeRequest.getValidTime().longValue()<0){
                throw new SmsException("010000","操作失败，不能输入负数");
            }
            i = rechargeTypeSevice.updateRecharge(rechargeTypeRequest, phone);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmRechargeTypeController.update",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "充值类别删除  门店端-充值类别-充值类别删除",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/delete")
    public GenericResponse delete(@RequestBody RechargeTypeRequest rechargeTypeRequest){
        GenericResponse<RechargeType> genericResponse = new GenericResponse<>();

        int i = 0;
        try {
            i = rechargeTypeSevice.deleteRecharge(rechargeTypeRequest);
        }catch (Exception e){
            log.error("SsmRechargeTypeController.delete",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }


    @ApiOperation(value = "充值类别启用停用   门店端-充值类别-充值类别启用停用",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/updateStates")
    public GenericResponse updateStates(@RequestBody RechargeTypeRequest rechargeTypeRequest){
        GenericResponse<RechargeType> genericResponse = new GenericResponse<>();

        int i = 0;
        try {
            i = rechargeTypeSevice.updateStates(rechargeTypeRequest);
        }catch (Exception e){
            log.error("SsmRechargeTypeController.updateStates",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0){
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }


    @ApiOperation(value = "查询充值类别List  门店端-充值单据-充值类别查询List",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/list")
    public GenericResponse selectList(@RequestBody RechargeTypeRequest rechargeTypeRequest){
        GenericResponse<RechargeType> genericResponse = new GenericResponse<>();
//        Long storeId = SessionHelp.getStoreId(session);
//        rechargeTypeRequest.setStoreId(storeId);
        List<RechargeType> list = new ArrayList<>();
        try {
            list = rechargeTypeSevice.selectList(rechargeTypeRequest,null);
        }catch (Exception e){
            log.error("SsmRechargeTypeController.selectList:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

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


}
