package com.hzs.ground.product.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzs.common.core.constant.msg.ProductMsgConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.poi.ExcelUtil;
import com.hzs.common.core.web.controller.BaseController;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.core.web.page.TableDataInfo;
import com.hzs.common.domain.scm.th.ThProductExtend;
import com.hzs.common.domain.scm.th.ThProductSpecs;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import com.hzs.ground.product.param.ProductInfoParam;
import com.hzs.ground.product.service.IThProductExtendService;
import com.hzs.ground.product.service.IThProductService;
import com.hzs.ground.product.service.IThProductSpecsService;
import com.hzs.ground.product.vo.ProductVo;
import com.hzs.system.base.ISystemConfigServiceApi;
import com.hzs.system.base.dto.BdCubasdocDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/manage/th-product")
public class ThProductController extends BaseController {

    @Autowired
    private IThProductService iThProductService;
    @Autowired
    private IThProductSpecsService iThProductSpecsService;
    @Autowired
    private IThProductExtendService iThProductExtendService;

    @DubboReference
    ISystemConfigServiceApi iSystemConfigServiceApi;

    /**
     * 供应商 数据
     */
    @GetMapping("list")
    public TableDataInfo list(ProductInfoParam productInfoParam) {
        startPage();
        if (productInfoParam.getStartCreateDate() != null) {
            productInfoParam.setStartCreateDate(DateUtils.getStartTime(productInfoParam.getStartCreateDate()));
        }
        if (productInfoParam.getEndCreateDate() != null) {
            productInfoParam.setEndCreateDate(DateUtils.getEndTime(productInfoParam.getEndCreateDate()));
        }
        // 根据登录人获取供应商，根据登录账号查找绑定的供应商，查询供应商表
        R<BdCubasdocDTO> bdCubasdocDate = iSystemConfigServiceApi.queryBdCubasdocByUser(SecurityUtils.getUserId());
        if (!bdCubasdocDate.isSuccess() || bdCubasdocDate.getData() == null) {
            return getDataTable(new ArrayList<>());
        }
        productInfoParam.setPkSupplier(bdCubasdocDate.getData().getPkSupplier());

        List<ProductVo> list = iThProductService.queryByList(productInfoParam);
        return getDataTable(list);
    }

    /**
     * 导出
     */
    @PostMapping("export")
    public void export(HttpServletResponse response, ProductInfoParam productInfoParam) {
        if (productInfoParam.getStartCreateDate() != null) {
            productInfoParam.setStartCreateDate(DateUtils.getStartTime(productInfoParam.getStartCreateDate()));
        }
        if (productInfoParam.getEndCreateDate() != null) {
            productInfoParam.setEndCreateDate(DateUtils.getEndTime(productInfoParam.getEndCreateDate()));
        }
        List<ProductVo> list = iThProductService.queryByList(productInfoParam);
        ExcelUtil<ProductVo> util = new ExcelUtil<>(ProductVo.class);
        util.exportExcel(response, list, "供应商产品导出");
    }

    /**
     * 查询产品详情
     */
    @GetMapping("detail")
    public AjaxResult detail(Integer productId) {
        ProductVo product = iThProductService.getProductInfoById(productId);
        if (product == null) {
            return AjaxResult.error(TransactionUtils.getContent(ProductMsgConstants.PRODUCT_NOT_REQUIRED));
        }
        ProductInfoParam productParams = BeanUtil.copyProperties(product, ProductInfoParam.class);

        ThProductExtend productExtend = iThProductExtendService.getOne(new QueryWrapper<ThProductExtend>().eq("PK_PRODUCT", productId));
        productParams.setIsPickUp(productExtend.getIsPickUp());
        productParams.setIsPrize(productExtend.getIsPrize());
        productParams.setIsBarCode(productExtend.getIsBarCode());
        productParams.setRepurchaseType(productExtend.getRepurchaseType());
        productParams.setIsMerge(productExtend.getIsMerge());
        productParams.setIsGift(productExtend.getIsGift());
        productParams.setIsLandTrans(productExtend.getIsLandTrans());
        productParams.setIsAirTrans(productExtend.getIsAirTrans());
        productParams.setIsOceanTrans(productExtend.getIsOceanTrans());
        productParams.setSort(productExtend.getSort());
        LambdaQueryWrapper<ThProductSpecs> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ThProductSpecs::getPkProduct, productId);
        List<Integer> pkSpecsType = new ArrayList<>();
        List<ThProductSpecs> productSpecList = iThProductSpecsService.list(queryWrapper);
        for (ThProductSpecs productSpecs : productSpecList) {
            pkSpecsType.add(productSpecs.getPkSpecsType());
        }
        productParams.setPkSpecsType(pkSpecsType);
        productParams.setSupplierName(product.getSupplierName());
        return AjaxResult.success(productParams);
    }


}
