package com.hzs.system.home.controller.manage;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hzs.common.core.annotation.Log;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.enums.EOperationBusiness;
import com.hzs.common.core.enums.EOperationMethod;
import com.hzs.common.core.enums.EOperationModule;
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.system.home.HoProductDetails;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.system.home.param.HoProductDetailsParam;
import com.hzs.system.home.service.IHoProductDetailsService;
import com.hzs.system.home.vo.HoProductDetailsVO;
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.Date;
import java.util.List;

/**
 * <p>
 * 商品展示-商品简介详情配置 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2022-10-12
 */
@Slf4j
@RestController
@RequestMapping("/manage/ho-product-details")
public class HoProductDetailsController extends BaseController {
    @Autowired
    private IHoProductDetailsService hoProductDetailsService;

    /**
     * @description: 查询商品简介详情列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [hoProductDetails]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @Log(module = EOperationModule.PRODUCT_CONFIG,business = EOperationBusiness.PRODUCT_CONFIG, method = EOperationMethod.SELECT)
    @GetMapping("/list")
    public TableDataInfo list(HoProductDetailsParam hoProductDetails) {
        startPage();
        List<HoProductDetailsVO> list = hoProductDetailsService.queryProductList(hoProductDetails);
        return getDataTable(list);
    }

    /**
     * @description:导出
     * @author: zhang jing
     * @date: 2023/7/19 16:09
     * @param: [response, hoProductDetails]
     * @return: void
     **/
    @Log(module = EOperationModule.PRODUCT_CONFIG,business = EOperationBusiness.PRODUCT_CONFIG, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HoProductDetailsParam hoProductDetails) {
        List<HoProductDetailsVO> list = hoProductDetailsService.queryProductList(hoProductDetails);
        ExcelUtil<HoProductDetailsVO> util = new ExcelUtil<>(HoProductDetailsVO.class);
        util.exportExcel(response, list, "商品配置");
    }


    /**
     * @description: 新增商品简介详情
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [hoProductDetails]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.PRODUCT_CONFIG,business = EOperationBusiness.PRODUCT_CONFIG, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody HoProductDetails hoProductDetails) {
        hoProductDetails.setPkCountry(1);
        if(StringUtils.isEmpty(hoProductDetails.getTitleName())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TITLE_NAME_NOT_NULL));
        }
        if(StringUtils.isEmpty(hoProductDetails.getTwoLevelTitle())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TWO_TITLE_NAME_NOT_NULL));
        }
        if(StringUtils.isNull(hoProductDetails.getPkAreaClassify())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_HO_PRODUCT_RANGE_NOT_NULL));
        }

        if(StringUtils.isEmpty(hoProductDetails.getCoverAddress())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.COVER_ADDRESS_NOT_NULL));
        }
        if(StringUtils.isEmpty(hoProductDetails.getPicture())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_PICTURE_NOT_NULL));
        }
        if(StringUtils.isEmpty(hoProductDetails.getContent())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_CONTENT_NOT_NULL));
        }
        hoProductDetails.setPkCreator(SecurityUtils.getUserId());
        hoProductDetails.setCreationTime(new Date());
        return toAjax(hoProductDetailsService.save(hoProductDetails));
    }


    /**
     * @description: 修改商品简介详情
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [hoProductDetails]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.PRODUCT_CONFIG,business = EOperationBusiness.PRODUCT_CONFIG, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody HoProductDetails hoProductDetails) {
        if(StringUtils.isEmpty(hoProductDetails.getTitleName())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TITLE_NAME_NOT_NULL));
        }
        if(StringUtils.isEmpty(hoProductDetails.getTwoLevelTitle())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TWO_TITLE_NAME_NOT_NULL));
        }
        if(StringUtils.isNull(hoProductDetails.getPkAreaClassify())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_HO_PRODUCT_RANGE_NOT_NULL));
        }

        if(StringUtils.isEmpty(hoProductDetails.getCoverAddress())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.COVER_ADDRESS_NOT_NULL));
        }
        if(StringUtils.isEmpty(hoProductDetails.getPicture())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_PICTURE_NOT_NULL));
        }
        if(StringUtils.isEmpty(hoProductDetails.getContent())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_CONTENT_NOT_NULL));
        }
        hoProductDetails.setPkModified(SecurityUtils.getUserId());
        hoProductDetails.setModifiedTime(new Date());
        return toAjax(hoProductDetailsService.updateById(hoProductDetails));
    }


    /**
     * @description: 查询单条个商品简介详情
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        HoProductDetails hoProductDetails = hoProductDetailsService.getById(pkId);
        return AjaxResult.success(hoProductDetails);
    }


    /**
     * @description: 删除商品简介详情
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.PRODUCT_CONFIG,business = EOperationBusiness.PRODUCT_CONFIG, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        HoProductDetails hoProductDetails = hoProductDetailsService.getById(pkId);
        UpdateWrapper<HoProductDetails> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("PK_ID", pkId);
        updateWrapper.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        updateWrapper.set("PK_MODIFIED", SecurityUtils.getUserId());
        updateWrapper.set("MODIFIED_TIME", new Date());
        return toAjax(hoProductDetailsService.update(updateWrapper));
    }
}
