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


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.utils.TransactionUtils;
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.ext.HoEnterpriseMissionExt;
import com.hzs.common.domain.system.home.HoEnterpriseMission;
import com.hzs.common.domain.system.home.HoVrArea;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.home.service.IHoVrAreaService;
import com.hzs.system.home.vo.HoEnterpriseMissionVO;
import com.hzs.system.home.vo.HoVrAreaVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * VR专区 前端控制器
 * </p>
 *
 * @author hzs
 * @since 2022-11-19
 */
@RestController
@RequestMapping("/manage/ho-vr-area")
public class HoVrAreaController extends BaseController {
    @Autowired
    private IHoVrAreaService vrAreaService;

    /**
     * @description: 查询VR专区列表
     * @author: zhang jing
     * @date: 2022/8/30 10:20
     * @param: [vrArea]
     * @return: com.hzs.common.core.web.page.TableDataInfo
     **/
    @GetMapping("/list")
    @Log(module = EOperationModule.VR_ZONE,business = EOperationBusiness.VR_ZONE, method = EOperationMethod.SELECT)
    public TableDataInfo list(HoEnterpriseMissionExt vrArea) {
        startPage();
        QueryWrapper<HoVrArea> queryWrapper = new QueryWrapper();
        if(StringUtils.isNotNull(vrArea.getStartDate())
                &&StringUtils.isNotNull(vrArea.getEndDate())){
            queryWrapper.between("CREATION_TIME", vrArea.getStartDate(),
                     DateUtils.afterDateTime(1, ChronoUnit.DAYS,vrArea.getEndDate()));
        }
        if(StringUtils.isNotNull(vrArea.getStartEffectDate())
                &&StringUtils.isNotNull(vrArea.getEndEffectDate())){
            queryWrapper.between("EFFECTIVE_DATE", vrArea.getStartEffectDate(),
                     DateUtils.afterDateTime(1, ChronoUnit.DAYS,vrArea.getEndEffectDate()));
        }
        if(StringUtils.isNotEmpty(vrArea.getTitleName())){
            queryWrapper.like("TITLE_NAME",vrArea.getTitleName());
        }
        queryWrapper.orderByAsc("SORT");
        List<HoVrArea> list = vrAreaService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * @description: 导出
     * @author: zhang jing
     * @date: 2023/8/21 11:19
     * @param: [response, enterpriseMission]
     * @return: void
     **/
    @Log(module = EOperationModule.VR_ZONE,business = EOperationBusiness.VR_ZONE, method = EOperationMethod.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HoEnterpriseMissionExt vrArea) {
        QueryWrapper<HoVrArea> queryWrapper = new QueryWrapper();
        if(StringUtils.isNotNull(vrArea.getStartDate())
                &&StringUtils.isNotNull(vrArea.getEndDate())){
            queryWrapper.between("CREATION_TIME", vrArea.getStartDate(),
                    DateUtils.afterDateTime(1, ChronoUnit.DAYS,vrArea.getEndDate()));
        }
        if(StringUtils.isNotNull(vrArea.getStartEffectDate())
                &&StringUtils.isNotNull(vrArea.getEndEffectDate())){
            queryWrapper.between("EFFECTIVE_DATE", vrArea.getStartEffectDate(),
                    DateUtils.afterDateTime(1, ChronoUnit.DAYS,vrArea.getEndEffectDate()));
        }
        if(StringUtils.isNotEmpty(vrArea.getTitleName())){
            queryWrapper.like("TITLE_NAME",vrArea.getTitleName());
        }
        queryWrapper.orderByAsc("SORT");
        List<HoVrArea> list = vrAreaService.list(queryWrapper);
        List<HoVrAreaVO> newlist=new ArrayList<>();
        list.forEach(item -> newlist.add(BeanUtil.copyProperties(item, HoVrAreaVO.class)));
        ExcelUtil<HoVrAreaVO> util = new ExcelUtil<>(HoVrAreaVO.class);
        util.exportExcel(response, newlist, "VR专区");
    }


    /**
     * @description: 新增VR专区
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [vrArea]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.VR_ZONE,business = EOperationBusiness.VR_ZONE, method = EOperationMethod.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody HoVrArea vrArea) {
        if(StringUtils.isEmpty(vrArea.getTitleName())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TITLE_NAME_NOT_NULL));
        }
        if(StringUtils.isEmpty(vrArea.getPicture())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_PICTURE_NOT_NULL));
        }
        if(StringUtils.isEmpty(vrArea.getLinkAddress())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_LINK_ADDRESS_NOT_NULL));
        }

        if(StringUtils.isNull(vrArea.getSort())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_SORT_NOT_NULL));
        }
//        if(StringUtils.isNull(vrArea.getLanguageType())){
//            return AjaxResult.error(ConfigMsgConstants.HO_LANGUAGE_TYPE_NOT_NULL);
//        }
        if(StringUtils.isNull(vrArea.getEffectiveDate())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.EFFECTIVE_DATE_NOT_NULL));
        }
        QueryWrapper<HoVrArea> queryWrapper = new QueryWrapper();
        queryWrapper.eq("SORT",vrArea.getSort());
//        queryWrapper.eq("LANGUAGE_TYPE",vrArea.getLanguageType());
        HoVrArea check =vrAreaService.getOne(queryWrapper);
        if(StringUtils.isNotNull(check)){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_SORT_NOT_REPEAT));
        }

        vrArea.setPkCreator(SecurityUtils.getUserId());
        vrArea.setCreationTime(new Date());
        return toAjax(vrAreaService.save(vrArea));
    }


    /**
     * @description: 修改VR专区
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [vrArea]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.VR_ZONE,business = EOperationBusiness.VR_ZONE, method = EOperationMethod.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody HoVrArea vrArea) {
        if(StringUtils.isEmpty(vrArea.getTitleName())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.TITLE_NAME_NOT_NULL));
        }
        if(StringUtils.isEmpty(vrArea.getPicture())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_PICTURE_NOT_NULL));
        }
        if(StringUtils.isEmpty(vrArea.getLinkAddress())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_LINK_ADDRESS_NOT_NULL));
        }

        if(StringUtils.isNull(vrArea.getSort())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_SORT_NOT_NULL));
        }
//        if(StringUtils.isNull(vrArea.getLanguageType())){
//            return AjaxResult.error(ConfigMsgConstants.HO_LANGUAGE_TYPE_NOT_NULL);
//        }
        if(StringUtils.isNull(vrArea.getEffectiveDate())){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.EFFECTIVE_DATE_NOT_NULL));
        }
        QueryWrapper<HoVrArea> queryWrapper = new QueryWrapper();
        queryWrapper.ne("PK_ID",vrArea.getPkId());
        queryWrapper.eq("SORT",vrArea.getSort());
//        queryWrapper.eq("LANGUAGE_TYPE",vrArea.getLanguageType());
        HoVrArea check =vrAreaService.getOne(queryWrapper);
        if(StringUtils.isNotNull(check)){
            return AjaxResult.error(TransactionUtils.getContent(ConfigMsgConstants.HO_SORT_NOT_REPEAT));
        }
        vrArea.setPkModified(SecurityUtils.getUserId());
        vrArea.setModifiedTime(new Date());
        return toAjax(vrAreaService.updateById(vrArea));
    }


    /**
     * @description: 查询单条个VR专区
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.VR_ZONE,business = EOperationBusiness.VR_ZONE, method = EOperationMethod.SELECT)
    @GetMapping("/getOne/{pkId}")
    public AjaxResult getOne(@PathVariable Long pkId) {
        HoVrArea vrArea = vrAreaService.getById(pkId);
        return AjaxResult.success(vrArea);
    }


    /**
     * @description: 删除VR专区
     * @author: zhang jing
     * @date: 2022/8/30 10:21
     * @param: [pkId]
     * @return: com.hzs.common.core.web.domain.AjaxResult
     **/
    @Log(module = EOperationModule.VR_ZONE,business = EOperationBusiness.VR_ZONE, method = EOperationMethod.DELETE)
    @DeleteMapping("/{pkId}")
    public AjaxResult delete(@PathVariable Long pkId) {
        UpdateWrapper<HoVrArea> 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(vrAreaService.update(updateWrapper));
    }
}
