package com.hzs.sale.all.controller.manage;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hzs.common.core.utils.StringUtils;
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.sale.all.AllProduct;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.sale.all.param.AllProductQueryParam;
import com.hzs.sale.all.service.IAllProductService;
import com.hzs.sale.all.vo.AllProductQueryVO;
import lombok.extern.slf4j.Slf4j;
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.Date;
import java.util.List;

/**
 * 全网产品控制器
 */
@RestController
@RequestMapping("/manage/all-product")
@Slf4j
public class AllProductController extends BaseController {

    @Autowired
    private IAllProductService iAllProductService;

    /**
     * 产品列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AllProductQueryParam param) {
        List<AllProductQueryVO> resultList = new ArrayList<>();

        startPage();
        LambdaQueryWrapper<AllProduct> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(param.getWmsCode())) {
            queryWrapper.like(AllProduct::getWmsCode, param.getWmsCode());
        }
        if (StringUtils.isNotEmpty(param.getProductName())) {
            queryWrapper.like(AllProduct::getProductName, param.getProductName());
        }
        queryWrapper.orderByAsc(AllProduct::getInventory);
        List<AllProduct> productList = iAllProductService.list(queryWrapper);

        if (CollectionUtil.isNotEmpty(productList)) {
            resultList = BeanUtil.copyToList(productList, AllProductQueryVO.class);
        }

        return getDataTable(resultList);
    }

    /**
     * 产品导出
     *
     * @param response
     * @param param
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, AllProductQueryParam param) {
        List<AllProductQueryVO> resultList = new ArrayList<>();
        LambdaQueryWrapper<AllProduct> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(param.getWmsCode())) {
            queryWrapper.like(AllProduct::getWmsCode, param.getWmsCode());
        }
        if (StringUtils.isNotEmpty(param.getProductName())) {
            queryWrapper.like(AllProduct::getProductName, param.getProductName());
        }
        queryWrapper.orderByAsc(AllProduct::getInventory);
        List<AllProduct> productList = iAllProductService.list(queryWrapper);

        if (CollectionUtil.isNotEmpty(productList)) {
            resultList = BeanUtil.copyToList(productList, AllProductQueryVO.class);
        }

        ExcelUtil<AllProductQueryVO> util = new ExcelUtil<>(AllProductQueryVO.class);
        util.exportExcel(response, resultList, "产品导出");
    }

    /**
     * 保存产品信息
     *
     * @param param
     * @return
     */
    @PostMapping("/save")
    public AjaxResult save(@RequestBody AllProduct param) {
        if (iAllProductService.count(Wrappers.<AllProduct>lambdaQuery()
                .eq(AllProduct::getWmsCode, param.getWmsCode())
        ) > 0) {
            return AjaxResult.error("仓储编号已存在");
        }
        param.setPkCreator(SecurityUtils.getUserId());
        return AjaxResult.success(iAllProductService.save(param));
    }

    /**
     * 修改产品信息
     *
     * @param param
     * @return
     */
    @PostMapping("/update")
    public AjaxResult update(@RequestBody AllProduct param) {
        if (null == param.getPkId()) {
            return AjaxResult.error("缺少参数");
        }
        if (iAllProductService.count(Wrappers.<AllProduct>lambdaQuery()
                .eq(AllProduct::getWmsCode, param.getWmsCode())
                .ne(AllProduct::getPkId, param.getPkId())
        ) > 0) {
            return AjaxResult.error("仓储编号已存在");
        }
        param.setInventory(null);
        param.setPkModified(SecurityUtils.getUserId());
        param.setModifiedTime(new Date());
        return AjaxResult.success(iAllProductService.updateById(param));
    }

    /**
     * 删除
     *
     * @param pkId
     * @return
     */
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Integer pkId) {
        iAllProductService.deleteProduct(pkId, SecurityUtils.getUserId());
        return AjaxResult.success();
    }

    /**
     * 修改库存数量
     *
     * @param param
     * @return
     */
    @PostMapping("/change-inventory")
    public AjaxResult changeInventory(@RequestBody AllProduct param) {
        if (null == param.getPkId() || null == param.getInventory()) {
            return AjaxResult.error("缺少参数");
        }
        param.setPkModified(SecurityUtils.getUserId());
        iAllProductService.changeInventory(param);
        return AjaxResult.success();
    }

}
