package com.xunmei.core.resumption.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xunmei.common.core.web.page.TableDataInfo;
import com.xunmei.common.security.utils.SecurityUtils;
import com.xunmei.core.resumption.domain.Protection;
import com.xunmei.core.resumption.domain.ProtectionLog;
import com.xunmei.core.resumption.dto.protection.ProtectionEditDto;
import com.xunmei.core.resumption.dto.protection.ProtectionLogPageDto;
import com.xunmei.core.resumption.dto.protection.ProtectionPageDto;
import com.xunmei.core.resumption.mapper.ProtectionLogMapper;
import com.xunmei.core.resumption.mapper.ProtectionMapper;
import com.xunmei.core.resumption.service.IProtectionService;
import com.xunmei.core.resumption.vo.protection.ProtectionLogPageVo;
import com.xunmei.core.resumption.vo.protection.ProtectionPageVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.TimeZone;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author oygj
 * @since 2022-09-11
 */
@Service
public class ProtectionServiceImpl extends ServiceImpl<ProtectionMapper, Protection> implements IProtectionService {
//    @Resource
//    private OrgService orgService;

    @Resource
    ProtectionLogMapper protectionLogMapper;

    @Override
    public TableDataInfo pageList(ProtectionPageDto request) {
        if(ObjectUtil.isNull(request.getOrgId())){
            request.setOrgId(SecurityUtils.getLoginUser().getOrgId());
        }
        Page<ProtectionPageVo> page = request.getPageDto();
        if(ObjectUtil.isNotEmpty( request.getDateRange()) && request.getDateRange().length==2){
            Date startTime = new DateTime(request.getDateRange()[0]).setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            Date endTime = new DateTime(request.getDateRange()[1]).setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            request.setStartTime(startTime);
            request.setEndTime(endTime);
        }
        //按规则分页
        page = this.baseMapper.pageList(page, request);

        TableDataInfo<ProtectionPageVo> data= TableDataInfo.success(page);
//        int order=request.getPage()*request.getSize();
//        for (Object row : data.getRows()) {
//            ((ProtectionPageVo)row).setOrder(++order);
//        }

        return  data;
    }

    @Override
    public Protection get(Long id) {
        return this.getById(id);
    }

    @Override
    public Long edit(ProtectionEditDto dto) {
        Protection protection = new Protection();
        if (dto.getId() != null) {
            protection = getById(dto.getId());
            BeanUtil.copyProperties(dto, protection);
            updateById(protection);
        } else {
            BeanUtil.copyProperties(dto, protection);
            save(protection);
        }

        return protection.getId();

    }

    @Override
    public boolean del(Long id) {
        int count = baseMapper.update(null, new LambdaUpdateWrapper<Protection>()
                .eq(Protection::getId, id)
                .set(Protection::getDeleted, 1));
        return count > 0;
    }


    @Override
    public TableDataInfo history(ProtectionLogPageDto request) {
        Page<ProtectionLogPageVo> page = request.getPageDto();

        if (request.getUpdateTime() != null && request.getUpdateTime().length > 0) {
            Date startTime = new DateTime(request.getUpdateTime()[0]).setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            Date endTime = new DateTime(request.getUpdateTime()[1]).setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
            request.setStartTime(startTime);
            request.setEndTime(endTime);
        }
        //按规则分页
        page = this.protectionLogMapper.pageLog(page, request);
        TableDataInfo<ProtectionPageVo> data= TableDataInfo.success(page);
//        int order=request.getPage()*request.getSize();
//        for (Object row : data.getRows()) {
//            ((ProtectionLogPageVo)row).setOrder(++order);
//        }
        return data;
    }

    @Override
    @Transactional
    public boolean updateStatus(Long protectionId, String status) {
        Date now = DateUtil.date();
        int count = baseMapper.update(null, new LambdaUpdateWrapper<Protection>()
                .eq(Protection::getId, protectionId)
                .set(Protection::getStatus, status)
                .set(Protection::getStatusUpdateTime, now));
        if (count > 0) {
            Protection p = getById(protectionId);
            ProtectionLog log = new ProtectionLog();
            BeanUtil.copyProperties(p, log);
            log.setId(null);
            log.setProtectionId(protectionId);
            log.setStatus(status);
            log.setStatusUpdateTime(now);
            log.setStatusUpdatorId(SecurityUtils.getLoginUser().getUserid());
            log.setStatusUpdatorName(SecurityUtils.getLoginUser().getName());
            protectionLogMapper.insert(log);
        }

        return  count>0;
    }
}

