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


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hzs.common.core.constant.msg.ConfigMsgConstants;
import com.hzs.common.core.enums.EDelFlag;
import com.hzs.common.core.utils.StringUtils;
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.HoBackground;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.system.home.service.IHoBackgroundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 首页背景 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2022-11-18
 */
@RestController
@RequestMapping("/manage/ho-background")
public class HoBackgroundController extends BaseController {

    @Autowired
    private IHoBackgroundService backgroundService;

    /**
     * @description: 查询首页背景列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [background]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @GetMapping("/list")
    public TableDataInfo list(HoBackground background) {
        startPage();
        List<HoBackground> list = backgroundService.list();
        return getDataTable(list);
    }


    /**
     * @description: 新增首页背景
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [background]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/save")
    public AjaxResult save(@RequestBody HoBackground background) {
        if(StringUtils.isNull(background.getVideoAddress())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.VIDEO_ADDRESS_NOT_NULL));
        }
        if(StringUtils.isNull(background.getCoverAddress())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.COVER_ADDRESS_NOT_NULL));
        }

        if(StringUtils.isNull(background.getEffectiveDate())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.EFFECTIVE_DATE_NOT_NULL));
        }

        background.setPkCreator(SecurityUtils.getUserId());
        background.setCreationTime(new Date());
        return toAjax(backgroundService.save(background));
    }


    /**
     * @description: 修改首页背景
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [background]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/update")
    public AjaxResult update(@RequestBody HoBackground background) {
        if(StringUtils.isNull(background.getVideoAddress())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.VIDEO_ADDRESS_NOT_NULL));
        }
        if(StringUtils.isNull(background.getCoverAddress())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.COVER_ADDRESS_NOT_NULL));
        }
        if(StringUtils.isNull(background.getEffectiveDate())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.EFFECTIVE_DATE_NOT_NULL));
        }
        background.setPkModified(SecurityUtils.getUserId());
        background.setModifiedTime(new Date());
        return toAjax(backgroundService.updateById(background));
    }


    /**
     * @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) {
        HoBackground background = backgroundService.getById(pkId);
        return AjaxResult.success(background);
    }


    /**
     * @description: 删除首页背景
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        UpdateWrapper<HoBackground> 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(backgroundService.update(updateWrapper));
    }
}
