package link.at17.mid.tushare.api.common;

import java.util.List;
import java.util.Objects;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.yulichang.query.MPJLambdaQueryWrapper;

import link.at17.mid.tushare.data.models.StockInfo;
import link.at17.mid.tushare.data.service.StockInfoService;
import link.at17.mid.tushare.enums.ListStatus;
import link.at17.mid.tushare.enums.StockMarket;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/api/v1/common/stockInfo")
@Slf4j
public class StockInfoController {
	
	@Autowired
	StockInfoService stockInfoService;
	
	@GetMapping("list")
	private List<StockInfo> list(
	        @RequestParam(name="listStatus", required=false) ListStatus listStatus,
	        @RequestParam(name="stockMarket", required=false) StockMarket[] stockMarket,
	        @RequestParam(name="ths", required=false, defaultValue="false") Boolean ths) {
	    MPJLambdaQueryWrapper<StockInfo> ew = new MPJLambdaQueryWrapper<StockInfo>();
        ew.eq(Objects.nonNull(listStatus), StockInfo::getListStatus, listStatus);
        if (ArrayUtils.isNotEmpty(stockMarket)) {
            ew.and(w -> {
                for (int i = 0; i < stockMarket.length; i++) {
                    w.likeLeft(StockInfo::getTsCode, stockMarket[i]);
                    if (i != stockMarket.length - 1) {
                        w.or();
                    }
                }
            });
        }
		return ths ? stockInfoService.list(ew) : stockInfoService.listWithoutThs(ew);
	}
}
