package com.sptg.gateway.service.impl;

import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.estate.api.dto.FaceChargeProjectDto;
import com.sptg.estate.api.entity.FaceChargeProject;
import com.sptg.estate.api.entity.Residential;
import com.sptg.estate.api.feigns.FaceChargeProjectFeign;
import com.sptg.estate.api.feigns.ResidentialFeign;
import com.sptg.gateway.service.CsvService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@RequiredArgsConstructor
public class FaceChargeProjectCsvServiceImpl implements CsvService {

    private final FaceChargeProjectFeign faceChargeProjectFeign;
    private final ResidentialFeign residentialFeign;

    @Override
    public String getFlag() {
        return "cvs_faceChangeProject";
    }

    @Override
    public String getTemplateName() {
        return "人脸套餐导入模板";
    }

    @Override
    public ResultBean<?> save(Object data) {
        return this.faceChargeProjectFeign.addFaceChargeProject((FaceChargeProjectDto) data);
    }

    @Override
    public Object arrToObj(String[] array) {
        FaceChargeProjectDto faceChargeProjectDto = new FaceChargeProjectDto();
        //原数据id
        String oldId = array[7];
        Assert.hasText(oldId,"原数据Id不能为空");
        faceChargeProjectDto.setOldId(oldId);
        FaceChargeProject faceChargeProject = this.faceChargeProjectFeign.getByOldId(oldId);
        if (faceChargeProject != null){
            return new ResultBean<>(ResultEnum.PARAMETER_ANOMALY,"该套餐已存在");
        }
        //小区id
        String oldResidentialId = array[6];
        Assert.notNull(oldResidentialId,"小区id不能为空");
        Residential residential = residentialFeign.getByOldId(oldResidentialId);
        if (residential == null){
            return new ResultBean<>(ResultEnum.NOT_FIND,"小区不存在");
        }
        faceChargeProjectDto.setResidentialId(residential.getId());
        //套餐名称
        String name = array[0];
        Assert.hasText(name,"套餐名称不能为空");
        faceChargeProjectDto.setName(name);
        //套餐类型
        Integer type = Integer.parseInt(array[1]);
        Assert.notNull(type,"套餐类型不能为空");
        faceChargeProjectDto.setType(type);
        //收费类型
        Integer typeItem = Integer.parseInt(array[2]);
        Assert.notNull(typeItem,"收费类型不能为空");
        faceChargeProjectDto.setTypeItem(typeItem);
        List<FaceChargeProject> faceChargeProjectList = this.faceChargeProjectFeign.getListByResidentialId(residential.getId());
        if (faceChargeProjectList.size() >0){
            for (FaceChargeProject chargeProject : faceChargeProjectList){
                if (chargeProject.getType().equals(OperationConstant.ONE) && chargeProject.getType().equals(type)){
                    return new ResultBean<>(ResultEnum.PARAMETER_ANOMALY,"导入套餐失败：该小区已存在套餐类型为免费的套餐");
                }else if (chargeProject.getType().equals(OperationConstant.TWO) && chargeProject.getTypeItem().equals(21) && type.equals(OperationConstant.TWO)){
                    return new ResultBean<>(ResultEnum.PARAMETER_ANOMALY,"导入套餐失败：该小区已存在套餐类型为收费，收费类型为押金的套餐");
                }
            }
        }
        //天数
        Integer days = Integer.parseInt(array[4]);
        Assert.notNull(days,"天数不能为空");
        faceChargeProjectDto.setDays(days);
        //金额
        //判断套餐类型，如果是免费类型则金额设置为零，折扣为100；如果是收费套餐则设置为传入的金额，收费类型为套餐时折扣设置为传入的折扣。
        if (type.equals(OperationConstant.ONE)){
            faceChargeProjectDto.setAmount(BigDecimal.ZERO);
            faceChargeProjectDto.setDiscount(100);
        }else {
            String amount = array[3];
            Assert.notNull(amount,"套餐类型为收费时，金额不能为空");
            faceChargeProjectDto.setAmount(new BigDecimal(amount));
            if (typeItem.equals(21)){
                faceChargeProjectDto.setDiscount(100);
            }else {
                if(StringUtils.hasText(array[5])){
                    faceChargeProjectDto.setDiscount(Integer.parseInt(array[5]));
                }else{
                    faceChargeProjectDto.setDiscount(100);
                }
            }
        }
        faceChargeProjectDto.setStatus(OperationConstant.ZERO);
        return faceChargeProjectDto;
    }

    private final static List<String> HEADER_LIST = new ArrayList<>(Arrays.asList(
            "套餐名称(必填)",
            "收费类型{1：免费，2：付费}(必填)",
            "套餐类型{11：试用，12：长期，21：押金，22：套餐}(必填)",
            "金额(单位为：元，小数为两位。套餐类型为付费时必填，其他不填)",
            "天数(必填)",
            "折扣{85，表示85%，即8.5折}(收费类型为套餐时选填，其他不填)",
            "原小区Id(必填)",
            "原套餐主键Id(必填)"
    ));

    @Override
    public List<String> getHeaderList() {
        return HEADER_LIST;
    }
}
