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


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.DateUtils;
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.ext.HoAdvertBannerExt;
import com.hzs.common.domain.system.home.HoAdvertBanner;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.common.core.utils.TransactionUtils;
import com.hzs.system.home.service.IHoAdvertBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 广告banner 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2022-12-08
 */
@RestController
@RequestMapping("/manage/ho-advert-banner")
public class HoAdvertBannerController extends BaseController {

    @Autowired
    private IHoAdvertBannerService advertBannerService;

    /**
     * @description: 查询广告banner
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [advertBanner]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @GetMapping("/list")
    public TableDataInfo list(HoAdvertBannerExt advertBannerExt) {
        startPage();
        QueryWrapper<HoAdvertBanner> queryWrapper = new QueryWrapper();
        if(StringUtils.isNotNull(advertBannerExt.getPosition())){
            queryWrapper.eq("POSITION",advertBannerExt.getPosition());
        }
        if(StringUtils.isNotNull(advertBannerExt.getStartDate())
                &&StringUtils.isNotNull(advertBannerExt.getEndDate())){
            queryWrapper.between("CREATION_TIME", advertBannerExt.getStartDate(),
                    DateUtils.afterDateTime(1, ChronoUnit.DAYS,advertBannerExt.getEndDate()));
        }
        if(StringUtils.isNotNull(advertBannerExt.getStartEffectDate())
                &&StringUtils.isNotNull(advertBannerExt.getEndEffectDate())){
            queryWrapper.between("EFFECTIVE_DATE", advertBannerExt.getStartEffectDate(),
                    DateUtils.afterDateTime(1, ChronoUnit.DAYS,advertBannerExt.getEndEffectDate()));
        }
        queryWrapper.orderByAsc("SORT");
        List<HoAdvertBanner> list = advertBannerService.list(queryWrapper);
        return getDataTable(list);
    }


    /**
     * @description: 新增广告banner
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [advertBanner]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/save")
    public AjaxResult save(@RequestBody HoAdvertBanner advertBanner) {

        if(StringUtils.isNull(advertBanner.getPosition())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_POSITION_NOT_NULL));
        }
        if(StringUtils.isNull(advertBanner.getSort())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_SORT_NOT_NULL));
        }
        if(StringUtils.isNull(advertBanner.getEffectiveDate())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.EFFECTIVE_DATE_NOT_NULL));
        }

        if(StringUtils.isEmpty(advertBanner.getLink())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_LINK_ADDRESS_NOT_NULL));
        }
        if(StringUtils.isEmpty(advertBanner.getPicture())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_PICTURE_NOT_NULL));
        }
        QueryWrapper<HoAdvertBanner> queryWrapper = new QueryWrapper();
        queryWrapper.eq("SORT",advertBanner.getSort());
        List<HoAdvertBanner> list = advertBannerService.list(queryWrapper);
        if(CollectionUtil.isNotEmpty(list)){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_SORT_NOT_REPEAT));
        }
        advertBanner.setPkCreator(SecurityUtils.getUserId());
        advertBanner.setCreationTime(new Date());
        return toAjax(advertBannerService.save(advertBanner));
    }


    /**
     * @description: 修改广告banner
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [advertBanner]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/update")
    public AjaxResult update(@RequestBody HoAdvertBanner advertBanner) {
        if(StringUtils.isNull(advertBanner.getPosition())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_POSITION_NOT_NULL));
        }
        if(StringUtils.isNull(advertBanner.getSort())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_SORT_NOT_NULL));
        }
        if(StringUtils.isNull(advertBanner.getEffectiveDate())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.EFFECTIVE_DATE_NOT_NULL));
        }

        if(StringUtils.isEmpty(advertBanner.getLink())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_LINK_ADDRESS_NOT_NULL));
        }
        if(StringUtils.isEmpty(advertBanner.getPicture())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_PICTURE_NOT_NULL));
        }

        advertBanner.setPkModified(SecurityUtils.getUserId());
        advertBanner.setModifiedTime(new Date());
        return toAjax(advertBannerService.updateById(advertBanner));
    }

    /**
     * @description: 修改为置顶
     * @author: zhang jing
     * @date: 2022/12/7 11:55
     * @param: [advertBanner]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/updateTopping")
    public AjaxResult updateTopping(@RequestBody HoAdvertBanner advertBanner) {
        if(StringUtils.isNull(advertBanner.getPkId())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_ID_NOT_NULL));
        }

        QueryWrapper<HoAdvertBanner> queryWrapper2 = new QueryWrapper();
        queryWrapper2.eq("PK_ID",advertBanner.getPkId());
        HoAdvertBanner topping =advertBannerService.getOne(queryWrapper2);
        if(StringUtils.isNull(topping)){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_OBJECT_NOT_REPEAT));
        }
        QueryWrapper<HoAdvertBanner> queryWrapper = new QueryWrapper();
        queryWrapper.orderByAsc("SORT");
        List<HoAdvertBanner> list = advertBannerService.list(queryWrapper);
        if(CollectionUtil.isNotEmpty(list)){
            HoAdvertBanner toppingOld =list.get(0);
            advertBanner.setSort(toppingOld.getSort());
            toppingOld.setSort(topping.getSort());
            toppingOld.setPkModified(SecurityUtils.getUserId());
            toppingOld.setModifiedTime(new Date());
            advertBannerService.updateById(toppingOld);
        }else{
            advertBanner.setSort(1);
        }
        advertBanner.setPkModified(SecurityUtils.getUserId());
        advertBanner.setModifiedTime(new Date());
        return toAjax(advertBannerService.updateById(advertBanner));
    }

    /**
     * @description: 上移
     * @author: zhang jing
     * @date: 2022/12/7 14:24
     * @param: [advertBanner]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/moveUp")
    public AjaxResult moveUp(@RequestBody HoAdvertBanner advertBanner) {
        if(StringUtils.isNull(advertBanner.getPkId())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_ID_NOT_NULL));
        }
        HoAdvertBanner advertBannerNew =advertBannerService.getById(advertBanner);
        if(StringUtils.isNull(advertBannerNew)){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_OBJECT_NOT_REPEAT));
        }
        QueryWrapper<HoAdvertBanner> queryWrapper = new QueryWrapper();
        queryWrapper.lt("SORT",advertBannerNew.getSort());
        queryWrapper.orderByDesc("SORT");
        List<HoAdvertBanner> list = advertBannerService.list(queryWrapper);
        if(CollectionUtil.isNotEmpty(list)){
            HoAdvertBanner toppingOld =list.get(0);
            advertBanner.setSort(toppingOld.getSort());
            toppingOld.setSort(advertBannerNew.getSort());
            toppingOld.setPkModified(SecurityUtils.getUserId());
            toppingOld.setModifiedTime(new Date());
            advertBannerService.updateById(toppingOld);
        }else{
            advertBanner.setSort(1);
        }
        advertBanner.setPkModified(SecurityUtils.getUserId());
        advertBanner.setModifiedTime(new Date());
        return toAjax(advertBannerService.updateById(advertBanner));
    }

    /**
     * @description: 下移
     * @author: zhang jing
     * @date: 2022/12/7 14:24
     * @param: [advertBanner]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @PostMapping("/moveDown")
    public AjaxResult moveDown(@RequestBody HoAdvertBanner advertBanner) {
        if(StringUtils.isNull(advertBanner.getPkId())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.PK_ID_NOT_NULL));
        }
        HoAdvertBanner advertBannerNew =advertBannerService.getById(advertBanner);
        if(StringUtils.isNull(advertBannerNew)){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_OBJECT_NOT_REPEAT));
        }
        QueryWrapper<HoAdvertBanner> queryWrapper = new QueryWrapper();
        queryWrapper.gt("SORT",advertBannerNew.getSort());
        queryWrapper.orderByAsc("SORT");
        List<HoAdvertBanner> list = advertBannerService.list(queryWrapper);
        if(CollectionUtil.isNotEmpty(list)){
            HoAdvertBanner toppingOld =list.get(0);
            advertBanner.setSort(toppingOld.getSort());
            toppingOld.setSort(advertBannerNew.getSort());
            toppingOld.setPkModified(SecurityUtils.getUserId());
            toppingOld.setModifiedTime(new Date());
            advertBannerService.updateById(toppingOld);
        }else{
            advertBanner.setSort(1);
        }
        advertBanner.setPkModified(SecurityUtils.getUserId());
        advertBanner.setModifiedTime(new Date());
        return toAjax(advertBannerService.updateById(advertBanner));
    }


    /**
     * @description: 查询单条个广告banner
     * @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) {
        HoAdvertBanner advertBanner = advertBannerService.getById(pkId);
        return AjaxResult.success(advertBanner);
    }


    /**
     * @description: 删除广告banner
     * @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<HoAdvertBanner> 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(advertBannerService.update(updateWrapper));
    }

}
