package com.fs.store.service.impl;

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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.fs.common.core.domain.R;
import com.fs.common.exception.CustomException;
import com.fs.common.utils.DateUtils;
import com.fs.store.domain.FsShippingTemplatesFree;
import com.fs.store.domain.FsShippingTemplatesRegion;
import com.fs.store.dto.AppointInfoDTO;
import com.fs.store.dto.RegionDTO;
import com.fs.store.dto.RegionInfoDTO;
import com.fs.store.mapper.FsShippingTemplatesFreeMapper;
import com.fs.store.mapper.FsShippingTemplatesRegionMapper;
import com.fs.store.param.FsShippingTemplatesAddEditParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fs.store.mapper.FsShippingTemplatesMapper;
import com.fs.store.domain.FsShippingTemplates;
import com.fs.store.service.IFsShippingTemplatesService;

/**
 * 运费模板Service业务层处理
 * 
 * @author fs
 * @date 2022-03-15
 */
@Service
public class FsShippingTemplatesServiceImpl implements IFsShippingTemplatesService 
{
    @Autowired
    private FsShippingTemplatesMapper fsShippingTemplatesMapper;

    @Autowired
    private FsShippingTemplatesFreeMapper fsShippingTemplatesFreeMapper;

    @Autowired
    private FsShippingTemplatesRegionMapper fsShippingTemplatesRegionMapper;



    /**
     * 查询运费模板
     * 
     * @param shippingId 运费模板ID
     * @return 运费模板
     */
    @Override
    public FsShippingTemplates selectFsShippingTemplatesById(Long id)
    {
        return fsShippingTemplatesMapper.selectFsShippingTemplatesById(id);
    }

    /**
     * 查询运费模板列表
     * 
     * @param fsShippingTemplates 运费模板
     * @return 运费模板
     */
    @Override
    public List<FsShippingTemplates> selectFsShippingTemplatesList(FsShippingTemplates fsShippingTemplates)
    {
        return fsShippingTemplatesMapper.selectFsShippingTemplatesList(fsShippingTemplates);
    }

    /**
     * 新增运费模板
     * 
     * @param fsShippingTemplates 运费模板
     * @return 结果
     */
    @Override
    public int insertFsShippingTemplates(FsShippingTemplates fsShippingTemplates)
    {
        fsShippingTemplates.setCreateTime(DateUtils.getNowDate());
        return fsShippingTemplatesMapper.insertFsShippingTemplates(fsShippingTemplates);
    }

    /**
     * 修改运费模板
     * 
     * @param fsShippingTemplates 运费模板
     * @return 结果
     */
    @Override
    public int updateFsShippingTemplates(FsShippingTemplates fsShippingTemplates)
    {
        fsShippingTemplates.setUpdateTime(DateUtils.getNowDate());
        return fsShippingTemplatesMapper.updateFsShippingTemplates(fsShippingTemplates);
    }

    /**
     * 批量删除运费模板
     * 
     * @param shippingIds 需要删除的运费模板ID
     * @return 结果
     */
    @Override
    public int deleteFsShippingTemplatesByIds(Long[] ids)
    {
        return fsShippingTemplatesMapper.deleteFsShippingTemplatesByIds(ids);
    }

    /**
     * 删除运费模板信息
     * 
     * @param shippingId 运费模板ID
     * @return 结果
     */
    @Override
    public int deleteFsShippingTemplatesById(Long ids)
    {
        return fsShippingTemplatesMapper.deleteFsShippingTemplatesById(ids);
    }

    @Override
    public R addOrEdit(FsShippingTemplatesAddEditParam fsShippingTemplates) {
        if(fsShippingTemplates.getAppoint()==1&& fsShippingTemplates.getAppointInfo().isEmpty()){
            throw new CustomException("请指定包邮地区");
        }
        FsShippingTemplates shippingTemplates = new FsShippingTemplates();
        BeanUtil.copyProperties(fsShippingTemplates,shippingTemplates);
        shippingTemplates.setRegionInfo(JSON.toJSONString(fsShippingTemplates.getRegionInfo()));
        shippingTemplates.setAppointInfo(JSON.toJSONString(fsShippingTemplates.getAppointInfo()));
        if(fsShippingTemplates.getId() != null && fsShippingTemplates.getId()  > 0){
            shippingTemplates.setId(fsShippingTemplates.getId());
            this.updateFsShippingTemplates(shippingTemplates);
        }else{
            shippingTemplates.setCreateTime(DateUtils.getNowDate());
            this.insertFsShippingTemplates(shippingTemplates);
        }

        this.saveRegion(fsShippingTemplates,shippingTemplates.getId());
        this.saveFreeReigion(fsShippingTemplates,shippingTemplates.getId());
        return R.ok();
    }

    @Override
    public List<FsShippingTemplates> selectFsShippingTemplatesByIds(String ids) {
        return fsShippingTemplatesMapper.selectFsShippingTemplatesByIds(ids);
    }

    /**
     * 保存模板设置的区域价格
     * @param param ShippingTemplatesDTO
     * @param tempId 运费模板id
     */
    private void saveRegion(FsShippingTemplatesAddEditParam param,Long tempId){
        fsShippingTemplatesRegionMapper.deleteFsShippingTemplatesRegionByTempId(tempId);
        List<RegionInfoDTO> regionInfo = param.getRegionInfo();
        for (RegionInfoDTO regionInfoDto : regionInfo){
            String uni = IdUtil.simpleUUID();
            if(regionInfoDto.getRegion() != null && !regionInfoDto.getRegion().isEmpty()){
                for (RegionDTO regionDto : regionInfoDto.getRegion()){
                    if(regionDto.getChildren() != null && !regionDto.getChildren().isEmpty()){
                        for (RegionDTO childrenDtp : regionDto.getChildren()){
                            FsShippingTemplatesRegion shippingTemplatesRegion = FsShippingTemplatesRegion.builder()
                                    .tempId(tempId)
                                    .first(new BigDecimal(regionInfoDto.getFirst()))
                                    .firstPrice(new BigDecimal(regionInfoDto.getPrice()))
                                    .continues(new BigDecimal(regionInfoDto.getContinues()))
                                    .continuePrice(new BigDecimal(regionInfoDto.getContinuePrice()))
                                    .type(param.getType())
                                    .provinceId(Long.valueOf(regionDto.getCityId()))
                                    .cityId(Long.valueOf(childrenDtp.getCityId()))
                                    .build();
                            fsShippingTemplatesRegionMapper.insertFsShippingTemplatesRegion(shippingTemplatesRegion);
                        }
                    }else{
                        FsShippingTemplatesRegion shippingTemplatesRegion = FsShippingTemplatesRegion.builder()
                                .tempId(tempId)
                                .first(new BigDecimal(regionInfoDto.getFirst()))
                                .firstPrice(new BigDecimal(regionInfoDto.getPrice()))
                                .continues(new BigDecimal(regionInfoDto.getContinues()))
                                .continuePrice(new BigDecimal(regionInfoDto.getContinuePrice()))
                                .type(param.getType())
                                .provinceId(Long.valueOf(regionDto.getCityId()))
                                .build();
                        fsShippingTemplatesRegionMapper.insertFsShippingTemplatesRegion(shippingTemplatesRegion);
                    }
                }
            }
        }
    }

    /**
     * 保存包邮区域
     * @param param ShippingTemplatesDto
     * @param tempId 模板id
     */
    private void saveFreeReigion(FsShippingTemplatesAddEditParam param,Long tempId){

        if(param.getAppointInfo() == null
                || param.getAppointInfo().isEmpty()){
            return;
        }
        fsShippingTemplatesFreeMapper.deleteFsShippingTemplatesFreeByTempId(tempId);
        List<AppointInfoDTO> appointInfo = param.getAppointInfo();
        for (AppointInfoDTO appointInfoDto : appointInfo){
            if(appointInfoDto.getPlace() != null && !appointInfoDto.getPlace().isEmpty()){
                for (RegionDTO regionDto : appointInfoDto.getPlace()){
                    if(regionDto.getChildren() != null && !regionDto.getChildren().isEmpty()){
                        for (RegionDTO childrenDto : regionDto.getChildren()){
                            FsShippingTemplatesFree shippingTemplatesFree = FsShippingTemplatesFree.builder()
                                    .tempId(tempId)
                                    .number(new BigDecimal(appointInfoDto.getNumber()))
                                    .price(new BigDecimal(appointInfoDto.getPrice()))
                                    .type(param.getType())
                                    .provinceId(Long.valueOf(regionDto.getCityId()))
                                    .cityId(Long.valueOf(childrenDto.getCityId()))
                                    .build();
                            fsShippingTemplatesFreeMapper.insertFsShippingTemplatesFree(shippingTemplatesFree);
                        }
                    }
                }
            }
        }
    }
}
