package com.ssm.controller;

import com.ssm.core.bean.GenericResponse;
import com.ssm.core.utils.QRCodeUtil;
import com.ssm.manage.entity.BaseStore;
import com.ssm.manage.entity.MemberCs;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.query.MemberCsRequest;
import com.ssm.manage.query.StoreRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmBaseStoreService;
import com.ssm.manage.service.SsmMemberCsService;
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.http.MediaType;
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.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;


@Api(value="门店会员评价controller",tags={"门店会员评价controller相关接口"})
@RestController
@RequestMapping("/demay/ssm/memberCs")
@Slf4j
public class SsmMemberCsController {
    @Autowired
    private SsmMemberCsService ssmMemberCsService;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

    @ApiOperation(value = "查询门店会员评价记录list  会员微信小程序-查询门店会员账户交易记录list",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/selectRecentRecord")
    public GenericResponse selectRecentRecord(@RequestBody MemberCsRequest memberCsRequest){
        GenericResponse<MemberCs> genericResponse = new GenericResponse<>();
        List<MemberCs> lists = new ArrayList<>();
        try {
            lists = ssmMemberCsService.selectRecentRecord(memberCsRequest);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmMemberCsController.selectRecentRecord:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(lists);
        return genericResponse;
    }

    @ApiOperation(value = "门店会员评价  会员微信小程序-门店会员评价",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/appraise")
    public GenericResponse appraise(@RequestBody MemberCsRequest memberCsRequest){
        GenericResponse<MemberCs> genericResponse = new GenericResponse<>();
        int i = 0;
        try {
            i = ssmMemberCsService.appraise(memberCsRequest);
        }catch (Exception e){
            log.error("SsmMemberCsController.appraise:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "查询门店员工会员评价记录list  会员微信小程序-查询门店员工会员评价记录list",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/selectMemberCsByStaff")
    public GenericResponse selectMemberCsByStaff(@RequestBody MemberCsRequest memberCsRequest){
        GenericResponse<MemberCs> genericResponse = new GenericResponse<>();
        List<MemberCs> lists = new ArrayList<>();
        try {
            lists = ssmMemberCsService.selectMemberCsByStaff(memberCsRequest);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmMemberController.selectMemberCsByStaff:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(lists);
        return genericResponse;
    }

    @ApiOperation(value = "门店获取门店信息二维码  平台端-门店管理-门店获取门店信息二维码",httpMethod = "POST",notes = "",response = GenericResponse.class)
    @RequestMapping(value = "/getQrCode", produces = MediaType.IMAGE_PNG_VALUE)
    public byte[] getQrCode(@RequestBody StoreRequest storeRequest, HttpServletResponse response) {
        BaseStore baseStore = new BaseStore();
        byte[] bytes = new byte[0];
        try {
            baseStore = ssmBaseStoreService.selectByPrimaryKey(storeRequest.getId());
            String content = baseStore.getId() + "-" + baseStore.getStoreNum() + "-" + baseStore.getStoreName();
            bytes = QRCodeUtil.encode(content, null, false, false);
        }catch (Exception e) {
            log.error("SsmBaseStoreController.getQrCode", e);
        }
        return bytes;
    }
}
