package com.hzs.activity.pick.controller.manage;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hzs.activity.pick.param.AcRetailPickProductParam;
import com.hzs.activity.pick.service.IAcRetailPickProductService;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.annotation.RepeatSubmitSimple;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.*;
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.activity.pick.AcRetailPickProduct;
import com.hzs.common.domain.activity.pick.ext.AcRetailPickProductExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.util.TransactionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @description: 新零售会员提货产品
 * @author: zhang jing
 * @date: 2025/1/12 15:25
 * @param:
 * @return:
 **/
@RestController
@RequestMapping("/manage/pick-product")
public class AcRetailPickProductController extends BaseController {

    @Autowired
    private IAcRetailPickProductService retailPickProductService;

    /**
     * @description: 会员提货产品产品列表
     * @author: zhang jing
     * @date: 2025/1/3 16:27
     * @param: [retailPickProduct]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.RETAIL_PICK_PRODUCT, business = EOperationBusiness.RETAIL_PICK_PRODUCT, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(AcRetailPickProductExt retailPickProduct) {
        retailPickProduct.setPkCountry(SecurityUtils.getPkCountry());
        retailPickProduct.setSystemType(SecurityUtils.getSystemType());

        startPage();
        List<AcRetailPickProductExt> list = retailPickProductService.retailPickProductList(retailPickProduct);
        if (CollectionUtil.isNotEmpty(list)) {
            list.forEach(itm -> {
                itm.setIsPutOnVal(EDealerYesNo.getLabelByIntValue(itm.getIsPutOn()));
            });
        }
        return getDataTable(list);
    }

    /**
     * @description: 导出
     * @author: zhang jing
     * @date: 2025/1/12 17:19
     * @param: [response, retailPickProduct]
     * @return: void
     **/
    @Log(module = EOperationModule.RETAIL_PICK_PRODUCT, business = EOperationBusiness.RETAIL_PICK_PRODUCT, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AcRetailPickProductExt retailPickProduct) {
        retailPickProduct.setPkCountry(SecurityUtils.getPkCountry());
        retailPickProduct.setSystemType(SecurityUtils.getSystemType());

        List<AcRetailPickProductExt> list = retailPickProductService.retailPickProductList(retailPickProduct);
        if (CollectionUtil.isNotEmpty(list)) {
            list.forEach(itm -> {
                itm.setIsPutOnVal(EDealerYesNo.getLabelByIntValue(itm.getIsPutOn()));
            });
        }
        ExcelUtil<AcRetailPickProductExt> util = new ExcelUtil<>(AcRetailPickProductExt.class);
        util.exportExcel(response, list, "提货商品");
    }

    /**
     * @description: 新增会员提货产品产品
     * @author: zhang jing
     * @date: 2025/1/3 15:00
     * @param: [retailPickProduct]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/save")
    @RepeatSubmitSimple
    @Log(module = EOperationModule.RETAIL_PICK_PRODUCT, business = EOperationBusiness.RETAIL_PICK_PRODUCT, method = EOperationMethod.INSERT)
    public AjaxResult saveCuBonusExpand(@RequestBody AcRetailPickProductExt retailPickProduct) {

        if (StringUtils.isNull(retailPickProduct.getPkProduct())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_PRODUCT_NOT_NULL));
        }
        if (StringUtils.isNull(retailPickProduct.getAvailableQuantity()) ||
                retailPickProduct.getAvailableQuantity() <= 0) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.AVAILABLE_QUANTITY_NOT_NULL));
        }

        if (StringUtils.isNull(retailPickProduct.getShowPrice())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.SHOW_PRICE_NOT_NULL));
        }
        //校验产品是否已经添加
        LambdaQueryWrapper<AcRetailPickProduct> acqe = new LambdaQueryWrapper<>();
        acqe.eq(AcRetailPickProduct::getPkProduct, retailPickProduct.getPkProduct());
        int check = retailPickProductService.count(acqe);
        if (check > 0) {
            return AjaxResult.error(ConfigMsgConstants.PK_PRODUCT_REPEAT);
        }
        retailPickProduct.setCreationTime(new Date());
        retailPickProduct.setPkCreator(SecurityUtils.getUserId());
        retailPickProduct.setPkCountry(SecurityUtils.getPkCountry());
        retailPickProduct.setSystemType(SecurityUtils.getSystemType());
        return AjaxResult.success(retailPickProductService.save(retailPickProduct));
    }

    /**
     * @description: 查询单条新零售会员提货产品产品信息
     * @author: zhang jing
     * @date: 2025/1/3 15:43
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        LambdaQueryWrapper<AcRetailPickProduct> acqe = new LambdaQueryWrapper<>();
        acqe.eq(AcRetailPickProduct::getPkId, pkId);
        AcRetailPickProduct retailPickProduct = retailPickProductService.getOne(acqe);
        return AjaxResult.success(retailPickProduct);
    }

    /**
     * @description: 修改会员提货产品产品
     * @author: zhang jing
     * @date: 2025/1/3 15:59
     * @param: [retailPickProduct]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.RETAIL_PICK_PRODUCT, business = EOperationBusiness.RETAIL_PICK_PRODUCT, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody AcRetailPickProductExt retailPickProduct) {
        if (StringUtils.isNull(retailPickProduct.getPkProduct())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_PRODUCT_NOT_NULL));
        }
        if (StringUtils.isNull(retailPickProduct.getAvailableQuantity()) ||
                retailPickProduct.getAvailableQuantity() <= 0) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.AVAILABLE_QUANTITY_NOT_NULL));
        }

        if (StringUtils.isNull(retailPickProduct.getShowPrice())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.SHOW_PRICE_NOT_NULL));
        }
        //校验产品是否已经添加
        LambdaQueryWrapper<AcRetailPickProduct> acqe = new LambdaQueryWrapper<>();
        acqe.eq(AcRetailPickProduct::getPkProduct, retailPickProduct.getPkProduct());
        acqe.ne(AcRetailPickProduct::getPkId, retailPickProduct.getPkId());
        int check = retailPickProductService.count(acqe);
        if (check > 0) {
            return AjaxResult.error(ConfigMsgConstants.PK_PRODUCT_REPEAT);
        }
        retailPickProduct.setPkModified(SecurityUtils.getUserId());
        retailPickProduct.setModifiedTime(new Date());
        return AjaxResult.success(retailPickProductService.updateById(retailPickProduct));
    }

    /**
     * @description: 提货商品上下架
     * @author: zhang jing
     * @date: 2025/1/12 17:30
     * @param: [retailPickProduct]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.RETAIL_PICK_PRODUCT, business = EOperationBusiness.RETAIL_PICK_PRODUCT, method = EOperationMethod.UPDATE)
    @PostMapping("/updateIsPutOn")
    public AjaxResult updateIsPutOn(@RequestBody AcRetailPickProductParam param) {
        if (StringUtils.isNull(param.getPkId())) {
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PKID_NOT_NULL));
        }
        LambdaUpdateWrapper<AcRetailPickProduct> at = new LambdaUpdateWrapper<>();
        at.eq(AcRetailPickProduct::getPkId, param.getPkId());
        at.set(AcRetailPickProduct::getIsPutOn, param.getIsPutOn());
        at.set(AcRetailPickProduct::getPkModified, SecurityUtils.getUserId());
        at.set(AcRetailPickProduct::getModifiedTime, new Date());
        return AjaxResult.success(retailPickProductService.update(at));
    }

    /**
     * @description: 删除会员提货产品产品
     * @author: zhang jing
     * @date: 2025/1/3 16:03
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.RETAIL_PICK_PRODUCT, business = EOperationBusiness.RETAIL_PICK_PRODUCT, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        LambdaUpdateWrapper<AcRetailPickProduct> at = new LambdaUpdateWrapper<>();
        at.eq(AcRetailPickProduct::getPkId, pkId);
        at.set(AcRetailPickProduct::getDelFlag, EDelFlag.DELETE.getValue());
        at.set(AcRetailPickProduct::getPkModified, SecurityUtils.getUserId());
        at.set(AcRetailPickProduct::getModifiedTime, new Date());
        return AjaxResult.success(retailPickProductService.update(at));
    }

}
