package com.hzs.member.store.controller.api;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.member.store.ext.CuStoreExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.member.store.param.StoreQueryParam;
import com.hzs.member.store.service.ICuStoreService;
import com.hzs.member.store.vo.CuStoreVO;
import com.hzs.member.store.vo.StoreQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 店铺设置 前端控制器
 *
 * @author hzs
 * @since 2023-05-04
 */
@RestController
@RequestMapping("/api/store")
public class ApiCuStoreController extends BaseController {

    @Autowired
    private ICuStoreService storeService;

    /**
     * @description: 查询店铺配置列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [custoreVO]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @GetMapping("/list")
    public TableDataInfo list(CuStoreVO custoreVO) {
        custoreVO.setPkCountry(SecurityUtils.getPkCountry());
        List<CuStoreVO> list = storeService.queryStoreList(custoreVO);
        for (CuStoreVO cuStoreVO : list) {
            cuStoreVO.setStoreName(cuStoreVO.getStoreName() + " (" + cuStoreVO.getMemberCode() + ")");
        }
        return getDataTable(list);
    }


    /**
     * 查询会员店铺列表
     *
     * @param param
     * @return
     */
    @GetMapping("/store-list")
    public TableDataInfo storeList(StoreQueryParam param) {
        if (null == param.getPkCountry()) {
            param.setPkCountry(SecurityUtils.getPkCountry());
        }
        // 返回的数据结果
        List<StoreQueryVO> resultList = new ArrayList<>();

        startPage();
        List<CuStoreExt> list = storeService.queryStoreApiList(param);
        if (CollectionUtil.isNotEmpty(list)) {
            resultList = BeanUtil.copyToList(list, StoreQueryVO.class);
            for (StoreQueryVO storeQueryVO : resultList) {
                String address = "";
                if (StringUtils.isNotEmpty(storeQueryVO.getProvinceVal())) {
                    address += storeQueryVO.getProvinceVal() + " ";
                }
                if (StringUtils.isNotEmpty(storeQueryVO.getCityVal())) {
                    address += storeQueryVO.getCityVal() + " ";
                }
                if (StringUtils.isNotEmpty(storeQueryVO.getAddress())) {
                    address += storeQueryVO.getAddress();
                }
                storeQueryVO.setAddress(address);
            }
        }

        TableDataInfo tableDataInfo = getDataTable(list);
        tableDataInfo.setRows(resultList);
        return tableDataInfo;
    }

}
