package cn.pluss.platform.controller;

import cn.pluss.platform.api.PageInfo;
import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.constants.ProjectConstant;
import cn.pluss.platform.entity.MerchantBaseInfo;
import cn.pluss.platform.entity.MerchantImage;
import cn.pluss.platform.entity.MerchantStore;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.merchant.MerchantBaseInfoService;
import cn.pluss.platform.merchantImage.MerchantImageService;
import cn.pluss.platform.merchantStore.MerchantStoreService;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.util.ParametersUtil;
import cn.pluss.platform.util.StringUtil;
import cn.pluss.platform.vo.StoreInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author DJH
 */
@RestController
@RequestMapping("/merchantStore")
@Api(value = "MerchantStore控制类", tags = {"MerchantStore接口"})
public class MerchantStoreController {
    @Setter(onMethod_ = {@Autowired})
    private MerchantStoreService merchantStoreService;
    @Setter(onMethod_ = {@Autowired})
    private MerchantBaseInfoService merchantBaseInfoService;
    @Setter(onMethod_ = {@Autowired})
    private MerchantImageService merchantImageService;
    @Setter(onMethod_ = {@Autowired})
    private UserAppService userAppService;

    @PostMapping
    @ApiOperation(tags = {"页面-首页", "页面-商户详情"}, value = "获取门店信息", notes = "返回分页过后的数据", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "MerchantStore", value = "查询需要的信息实例", paramType = "body", dataType = "MerchantStore")})
    public Result<PageInfo<MerchantStore>> list(@RequestBody MerchantStore merchantStore) {
        UserApp userApp = userAppService.queryUserAppByToken();
        merchantStore.setMerchantCode(userApp.getMerchantCode());

        //判断有无商家信息
        if (merchantStore.getMerchantCode() == null || StringUtil.isEmpty(merchantStore.getMerchantCode())) {
            return ResultGenerator.genFailResult("无商家编码");
        }

        if (merchantStore.getSize() == null || merchantStore.getSize() == 0) {
            merchantStore.setSize(ProjectConstant.PAGE_SIZE);
        }
        if (merchantStore.getPage() == null || merchantStore.getPage() == 0) {
            merchantStore.setPage(1);
        }

        Map<String, Object> map = new HashMap<>();
        map.put("pageSize", merchantStore.getSize());
        map.put("offset", (merchantStore.getPage() - 1) * merchantStore.getSize());
        map.put("merchantCode", merchantStore.getMerchantCode());
        if (!StringUtil.isEmpty(merchantStore.getStatus())) {
            map.put("status", merchantStore.getStatus());
        }
        if (!StringUtil.isEmpty(merchantStore.getStoreName())) {
            map.put("storeName", merchantStore.getStoreName());
        }

        MerchantBaseInfo merchantBaseInfo = new MerchantBaseInfo();
        merchantBaseInfo.setMerchantCode(merchantStore.getMerchantCode());
        merchantBaseInfo = merchantBaseInfoService.queryMerchantBaseInfo(merchantBaseInfo);
        if (merchantBaseInfo == null) {
            return ResultGenerator.genSuccessResult(null);
        }

        List<MerchantStore> merchantStoreList = merchantStoreService.queryMerchantStorePage(map);

        for (MerchantStore m : merchantStoreList) {
            if (!StringUtil.isEmpty(merchantBaseInfo.getAlias())) {
                m.setAlias(merchantBaseInfo.getAlias());
            }
            if (!StringUtil.isEmpty(merchantBaseInfo.getProductDesc())) {
                m.setProductDesc(merchantBaseInfo.getProductDesc());
            }

            String merchantCode = merchantBaseInfo.getMerchantCode();
            if (!StringUtil.isEmpty(merchantCode)) {
                MerchantImage merchantImage = new MerchantImage();
                merchantImage.setMerchantCode(merchantCode);
                merchantImage.setPhotoType("06");
                List<MerchantImage> queryMerchantImageList = merchantImageService.queryMerchantImageList(merchantImage);
                if (null != queryMerchantImageList && !queryMerchantImageList.isEmpty()) {
                    merchantImage = queryMerchantImageList.get(0);
                    m.setStoreHeadPic(merchantImage.getPicUrl());
                }
                MerchantImage merchantImage1 = new MerchantImage();
                merchantImage1.setMerchantCode(merchantCode);
                merchantImage1.setPhotoType("08");
                List<MerchantImage> queryMerchantImageList1 = merchantImageService.queryMerchantImageList(merchantImage1);
                if (null != queryMerchantImageList1 && !queryMerchantImageList1.isEmpty()) {
                    merchantImage1 = queryMerchantImageList1.get(0);
                    m.setCashPic(merchantImage1.getPicUrl());
                }
                MerchantImage merchantImage2 = new MerchantImage();
                merchantImage2.setMerchantCode(merchantCode);
                merchantImage2.setPhotoType("09");
                List<MerchantImage> queryMerchantImageList2 = merchantImageService.queryMerchantImageList(merchantImage2);
                if (null != queryMerchantImageList2 && !queryMerchantImageList2.isEmpty()) {
                    merchantImage2 = queryMerchantImageList2.get(0);
                    m.setStoreInsidePic(merchantImage2.getPicUrl());
                }
            }
        }

        Integer count = merchantStoreService.queryMerchantStorePageCount(map);
        // 返回参数
        PageInfo<MerchantStore> pager = new PageInfo<>(count.longValue(), (long) StringUtil.getPageCount(count, merchantStore.getSize()),
                merchantStore.getPage().longValue(), merchantStore.getSize().longValue(), merchantStoreList);
        return ResultGenerator.genSuccessResult(pager);
    }

    @GetMapping("/{storeId}")
    @ApiOperation(value = "获取门店详情信息", notes = "查看单个项目的内容", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeId", value = "查询的storeId", paramType = "path", required = true, dataType = "String", defaultValue = "0")})
    public Result<MerchantStore> detail(@PathVariable String storeId) {
        MerchantStore merchantStore = new MerchantStore();
        merchantStore.setStoreId(storeId);
        merchantStore = merchantStoreService.queryMerchantStore(merchantStore);
        return ResultGenerator.genSuccessResult(merchantStore);
    }

    @GetMapping("/certInfo")
    public Result<Map<String, Object>> getCertInfo() {
        UserApp userApp = userAppService.queryUserAppByToken();
        Map<String, Object> certInfo = merchantStoreService.getCertInfo(userApp.getUserId() + "");
        return ResultGenerator.genSuccessResult(certInfo);
    }

    /**
     * 查询详情
     */
    @GetMapping("/showMerchantStoreReceipt")
    @ResponseBody
    public Result<Object> showMerchantStoreReceipt(Integer userId, Integer channel) {
        Map<String, Object> result = new HashMap<>(3);
        if (userId != null && userId != 0) {
            StoreInfoVO queryStoreInfoVO = merchantStoreService.showMerchantStoreReceipt(userId);
            String url = ParametersUtil.domain + "/wap/merchant/authPay?userAppId=" + queryStoreInfoVO.getUserId();
            result.put("url", url);
            result.put("storeName", queryStoreInfoVO.getStoreName());
            return ResultGenerator.genSuccessResult(result);
        }
        return ResultGenerator.genFailResult("获取失败");
    }
}
