package com.slodon.b2b2c.controller.system.seller;

import com.slodon.b2b2c.core.constant.ExpressConst;
import com.slodon.b2b2c.core.controller.BaseController;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.PageVO;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.core.util.UserUtil;
import com.slodon.b2b2c.core.util.WebUtil;
import com.slodon.b2b2c.model.seller.StoreBindExpressModel;
import com.slodon.b2b2c.model.system.ExpressModel;
import com.slodon.b2b2c.seller.example.StoreBindExpressExample;
import com.slodon.b2b2c.seller.pojo.StoreBindExpress;
import com.slodon.b2b2c.seller.pojo.Vendor;
import com.slodon.b2b2c.system.example.ExpressExample;
import com.slodon.b2b2c.system.pojo.Express;
import com.slodon.b2b2c.util.FaceSheet100NeedField;
import com.slodon.b2b2c.util.FaceSheetNeedField;
import com.slodon.b2b2c.vo.system.ExpressVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Slodon
 */
@Api(tags = "seller-物流公司")
@RestController
@RequestMapping("v3/system/seller/express")
public class SellerExpressController extends BaseController {

    @Resource
    private ExpressModel expressModel;
    @Resource
    private StoreBindExpressModel storeBindExpressModel;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation("物流公司列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "expressName", value = "物流名称", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("expressList")
    public JsonResult<PageVO<ExpressVO>> getExpressList(HttpServletRequest request, String expressName) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        String expressType = stringRedisTemplate.opsForValue().get("express_type");

        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        ExpressExample example = new ExpressExample();
        example.setExpressState(ExpressConst.EXPRESS_STATE_OPEN);
        example.setExpressNameLike(expressName);

        StringBuilder stringBuilder = new StringBuilder();
        //获取店铺内的所有快递公司
        StoreBindExpressExample storeBindExpressExample = new StoreBindExpressExample();
        storeBindExpressExample.setStoreId(vendor.getStoreId());
        List<StoreBindExpress> storeBindExpressList = storeBindExpressModel.getStoreBindExpressList(storeBindExpressExample, null);
        if (!CollectionUtils.isEmpty(storeBindExpressList)) {
            for (StoreBindExpress storeBindExpress : storeBindExpressList) {
                stringBuilder.append(",");
                stringBuilder.append(storeBindExpress.getExpressId());
            }
            example.setExpressIdNotIn(stringBuilder.substring(1));
        }
        if (!StringUtil.isEmpty(expressType)) {
            example.setSource(Integer.parseInt(expressType));
        }
        example.setOrderBy("sort asc, express_id desc");
        List<Express> list = expressModel.getExpressList(example, pager);
        List<ExpressVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (Express express : list) {
                ExpressVO vo = new ExpressVO(express);
                if ("1".equals(expressType)) {
                    vo.setIsSupportFaceSheet(FaceSheetNeedField.isHasExpressCode(express.getExpressCode()) ? 1 : 0);
                } else {
                    vo.setIsSupportFaceSheet(FaceSheet100NeedField.isHasExpressCode(express.getExpressCode()) ? 1 : 0);
                }
                vos.add(vo);
            }
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }
}
