package com.gic.haoban.manage.web.controller.coupon;

import com.gic.api.base.commons.BasePageInfo;
import com.gic.api.base.commons.Page;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.commons.util.EntityUtil;
import com.gic.commons.util.PageHelperUtils;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.haoban.manage.web.vo.coupon.CoupCardClerkLogCountVO;
import com.gic.haoban.manage.web.vo.coupon.CoupCardClerkLogVO;
import com.gic.haoban.manage.web.vo.coupon.CoupCardVO;
import com.gic.marketing.api.dto.CoupCardDTO;
import com.gic.marketing.api.dto.card.CoupCardClerkLogDTO;
import com.gic.marketing.api.out.service.CouponHBOuterService;
import com.gic.marketing.api.qdto.card.HaobanWriteOffCardQDTO;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 卡券
 */
@RestController
@RequestMapping("/coupon")
public class CouponController {

    private static final org.apache.logging.log4j.Logger logger = LogManager.getLogger(CouponController.class);

    @Autowired
    private CouponHBOuterService couponHBOuterService;

    /**
     * 卡券导购核销
     * @param cardLogId 卡券日志id
     * @param enterpriseId  企业id
     * @param clerkId   导购id
     * @return
     */
    @RequestMapping("/clerk-write-off")
    public RestResponse<CoupCardVO> writeOff(String cardLogId,
                                       String enterpriseId,String clerkId) {
        HaobanWriteOffCardQDTO writeOffCardQDTO = new HaobanWriteOffCardQDTO();
        writeOffCardQDTO.setEnterpriseId(enterpriseId);
        writeOffCardQDTO.setCardLogId(cardLogId);
        writeOffCardQDTO.setClerkId(clerkId);
        writeOffCardQDTO.setCardType(2);
        ServiceResponse<CoupCardClerkLogDTO> response = couponHBOuterService.scanWriteOff(writeOffCardQDTO);
        if (!response.isSuccess()) {
            return RestResponse.failure(response.getCode(), response.getMessage());
        }
        CoupCardVO cardVO = getCardVO(response.getResult());
        return RestResponse.successResult(cardVO);
    }

    /**
     * 卡券导购核销列表
     * @param pageInfo  分页参数
     * @param enterpriseId  企业id
     * @param clerkId   导购id
     * @param writeOffType  核销类型 1客户核销2导购核销
     * @return
     */
    @RequestMapping("/clerk-list")
    public RestResponse<Page<CoupCardClerkLogVO>> list(BasePageInfo pageInfo,
                                     String enterpriseId,String clerkId,Integer writeOffType) {
        Page<CoupCardClerkLogDTO> page = new Page<>();
        page.setCurrentPage(pageInfo.getPageNum());
        page.setPageSize(pageInfo.getPageSize());
        Map<String, Object> params = new HashMap<>();
        params.put("clerkId", clerkId);
        params.put("writeOffType", writeOffType);
        page.setParams(params);
        ServiceResponse<Page<CoupCardClerkLogDTO>> response = couponHBOuterService.pageClerkLog(enterpriseId, page);
        if (!response.isSuccess()) {
            return RestResponse.failure(response.getCode(), response.getMessage());
        }
        Page<CoupCardClerkLogVO> retPage = PageHelperUtils.changePageToCurrentPage(response.getResult(), CoupCardClerkLogVO.class);
        return RestResponse.successResult(retPage);
    }

    /**
     * 卡券导购核销详情
     * @param clerkLogId
     * @return
     */
    @RequestMapping("/clerk-get")
    public RestResponse<CoupCardVO> get(String clerkLogId) {
        ServiceResponse<CoupCardClerkLogDTO> response = couponHBOuterService.getClerkLogById(clerkLogId);
        if (!response.isSuccess()) {
            return RestResponse.failure(response.getCode(), response.getMessage());
        }
        if (response.getResult() == null) {
            return RestResponse.successResult();
        }
        CoupCardVO coupCardVO = getCardVO(response.getResult());
        return RestResponse.successResult(coupCardVO);
    }

    private static CoupCardVO getCardVO(CoupCardClerkLogDTO result) {
        if (result == null) {
            return null;
        }
        CoupCardVO coupCardVO = EntityUtil.changeEntityNew(CoupCardVO.class, result);
        CoupCardDTO coupCardDTO = result.getCoupCardDTO();
        if (coupCardDTO != null) {
            coupCardVO.setUseCondition(coupCardDTO.getUseCondition());
            coupCardVO.setEffectTime(coupCardDTO.getEffectTime());
            coupCardVO.setLimitTime(coupCardDTO.getLimitTime());
            coupCardVO.setStatus(coupCardDTO.getStatus());
            coupCardVO.setThirdName(coupCardDTO.getThirdName());
            coupCardVO.setMemberName(coupCardDTO.getMemberName());
            coupCardVO.setPhoneNum(coupCardDTO.getPhoneNum());
            coupCardVO.setCardNum(coupCardDTO.getCardNum());
        }
        coupCardVO.setUseTime(result.getCreateTime());
        return coupCardVO;
    }

    /**
     * 卡券核销数量
     * @param enterpriseId  企业id
     * @param clerkId   导购id
     * @return
     */
    @RequestMapping("/clerk-count")
    public RestResponse<CoupCardClerkLogCountVO> get(String enterpriseId, String clerkId) {
        ServiceResponse<Integer> memberResponse = couponHBOuterService.countClerkLog(enterpriseId, clerkId, 1);
        ServiceResponse<Integer> clerkResponse = couponHBOuterService.countClerkLog(enterpriseId, clerkId, 2);
        Integer memberCount = memberResponse.getResult();
        Integer clerkCount = clerkResponse.getResult();
        CoupCardClerkLogCountVO countVO = new CoupCardClerkLogCountVO();
        countVO.setMemberCount(memberCount == null ? 0 :memberCount);
        countVO.setClerkCount(clerkCount == null ? 0 :clerkCount);
        return RestResponse.successResult(countVO);
    }
}
