package com.sptg.estate.biz.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.common.core.util.JsonUtils;
import com.sptg.estate.api.dto.FaceChargeProjectDto;
import com.sptg.estate.api.entity.FaceChargeProject;
import com.sptg.estate.biz.mapper.FaceChargeProjectMapper;
import com.sptg.estate.biz.service.FaceChargeProjectServiceInterface;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * title: UnitServiceImpl
 * package: com.sptg.estate.biz.security.impl
 * description: 人脸套餐 服务层实现层
 *
 */
@Service
@RequiredArgsConstructor
public class FaceChargeProjectServiceImpl extends ServiceImpl<FaceChargeProjectMapper, FaceChargeProject> implements FaceChargeProjectServiceInterface {

    private final RedisTemplate<String,String> redisTemplate;
    private final static String FACE_CHARGE_PROJECT = "sptg:face:charge:project:";

    @Override
    public ResultBean<Page<FaceChargeProject>> showFaceChargeProjectList(FaceChargeProjectDto dto) {
        Wrapper<FaceChargeProject> wrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())){
            wrapper.like("s_name",dto.getName());
        }
        if (dto.getType() != null){
            wrapper.eq("i_type",dto.getType());
        }
        if (dto.getResidentialId() != null){
            wrapper.eq("l_residential_id",dto.getResidentialId());
        }
        wrapper.eq("i_status",OperationConstant.ZERO);
        return new ResultBean<>(ResultEnum.SUCCESS,this.selectPage(dto.getPage(),wrapper));
    }

    @Override
    public List<FaceChargeProject> getListByResidentialId(Long residentialId) {
        Wrapper<FaceChargeProject> wrapper = new EntityWrapper<>();
        wrapper.eq("l_residential_id",residentialId);
        wrapper.notIn("i_status",OperationConstant.MINUS_ONE);
        return this.selectList(wrapper);
    }
    @Override
    public List<FaceChargeProject> getListByResidentialIdAndDate(Long residentialId) {
        Wrapper<FaceChargeProject> wrapper = new EntityWrapper<>();
        wrapper.eq("l_residential_id",residentialId);
        wrapper.notIn("i_status",OperationConstant.MINUS_ONE);
        wrapper.gt("t_end_date",new Date());
        wrapper.eq("i_put_status",OperationConstant.ONE);
        return this.selectList(wrapper);
    }

    @Override
    public ResultBean<FaceChargeProject> selectByResidentialId(Long residentialId) {
        Wrapper<FaceChargeProject> wrapper = new EntityWrapper<>();
        wrapper.eq("l_residential_id",residentialId);
        wrapper.notIn("i_status",OperationConstant.MINUS_ONE);
        return new ResultBean<>(ResultEnum.SUCCESS,this.selectList(wrapper));
    }

    @Override
    public List<FaceChargeProject> getList(FaceChargeProjectDto dto) {
        Wrapper<FaceChargeProject> wrapper = new EntityWrapper<>();
        wrapper.notIn("i_status",OperationConstant.MINUS_ONE);
        if (dto.getResidentialId() != null){
            wrapper.eq("l_residential_id",dto.getResidentialId());
        }
        if (dto.getType() != null){
            wrapper.eq("i_type",dto.getType());
        }
        if (dto.getTypeItem() != null){
            wrapper.eq("i_type_item",dto.getTypeItem());
        }
        wrapper.eq(dto.getPutStatus() != null,"i_put_status",dto.getPutStatus());
        return this.selectList(wrapper);
    }

    @Override
    public ResultBean<FaceChargeProject> getOneFaceChargeProject(FaceChargeProjectDto dto) {
        Wrapper<FaceChargeProject> wrapper = new EntityWrapper<>();
        if (dto.getId() != null){
            wrapper.eq("l_id",dto.getId());
        }
        if (StringUtils.isNotBlank(dto.getName())){
            wrapper.like("s_name",dto.getName());
        }
        if (dto.getType() != null){
            wrapper.eq("i_type",dto.getType());
        }
        if (dto.getTypeItem() != null){
            wrapper.eq("i_type_item",dto.getTypeItem());
        }
        if (dto.getResidentialId() != null){
            wrapper.eq("l_residential_id",dto.getResidentialId());
        }
        wrapper.eq("i_status",OperationConstant.ZERO);
        FaceChargeProject faceChargeProject = this.selectOne(wrapper);
        return new ResultBean<>(ResultEnum.SUCCESS,faceChargeProject);
    }

    @Override
    public FaceChargeProject getOneNotInId(FaceChargeProjectDto dto) {
        Wrapper<FaceChargeProject> wrapper = new EntityWrapper<>();
        if (dto.getId() != null){
            wrapper.notIn("l_id",dto.getId());
        }
        if (dto.getName() != null){
            wrapper.eq("s_name",dto.getName());
        }
        wrapper.eq("l_residential_id",dto.getResidentialId());
        wrapper.notIn("i_status",OperationConstant.MINUS_ONE);
        return this.selectOne(wrapper);
    }

    @Override
    public ResultBean<Boolean> addFaceChargeProject(FaceChargeProjectDto dto) {

        FaceChargeProject faceChargeProject = new FaceChargeProject();
        BeanUtils.copyProperties(dto,faceChargeProject);
        faceChargeProject.setStatus(OperationConstant.ZERO);
        if(dto.getResidentialId() == null){
            return new ResultBean<>(ResultEnum.ERROR,false);
        }
        faceChargeProject.setResidentialId(dto.getResidentialId());
        if (!this.insert(faceChargeProject)){
            return new ResultBean<>(ResultEnum.ERROR,false);
        }
        Long date = faceChargeProject.getEndDate().getTime() - new Date().getTime();
        ValueOperations<String, String> operations = this.redisTemplate.opsForValue();
        String key = FACE_CHARGE_PROJECT + faceChargeProject.getId().toString();
        String json = JsonUtils.objectToJson(faceChargeProject);
        operations.set(key,json,date, TimeUnit.MILLISECONDS);
        return new ResultBean<>(ResultEnum.SUCCESS,true);
    }

    @Override
    public FaceChargeProject getFaceChargeProjectById(Long id) {
        return this.selectById(id);
    }

    @Override
    public ResultBean<Boolean> updateFaceChargeProject(FaceChargeProjectDto dto) {

        FaceChargeProject faceChargeProject = this.selectById(dto.getId());

        if (!dto.getTypeItem().equals(22) && !dto.getDiscount().equals(100)){
            return new ResultBean<>(ResultEnum.ERROR, false);
        }

        BeanUtils.copyProperties(dto,faceChargeProject);
        if (!this.updateById(faceChargeProject)){
            return new ResultBean<>(ResultEnum.ERROR, false);
        }
        return new ResultBean<>(ResultEnum.SUCCESS, true);
    }

    @Override
    public ResultBean<Boolean> deleteFaceChargeProject(FaceChargeProjectDto dto) {

        FaceChargeProject faceChargeProject = this.selectById(dto.getId());
        faceChargeProject.setStatus(OperationConstant.MINUS_ONE);
        if (this.updateById(faceChargeProject)){
            return new ResultBean<>(ResultEnum.SUCCESS,true);
        }else {
            return new ResultBean<>(ResultEnum.ERROR,false);
        }
    }

    @Override
    public FaceChargeProject getByOldId(String oldId) {
        Wrapper<FaceChargeProject> wrapper = new EntityWrapper<>();
        wrapper.eq("l_old_id",oldId);
        return this.selectOne(wrapper);
    }
}
