package com.fjhx.service.supplier.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.entity.supplier.Supplier;
import com.fjhx.enums.supplier.AccountPeriodTypeEnum;
import com.fjhx.mapper.supplier.SupplierMapper;
import com.fjhx.params.supplier.SupplierVo;
import com.fjhx.service.supplier.SupplierService;
import com.fjhx.uitl.code.CodeEnum;
import com.fjhx.utils.FileClientUtil;
import com.fjhx.utils.RegionClientUtil;
import com.fjhx.utils.wrapperUtil.IWrapper;
import com.fjhx.utils.wrapperUtil.KeywordData;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 供应商 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-11-24
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements SupplierService {

    /**
     * 默认供应商编码前缀
     */
    private final String codePrefix = "GY";

    @Override
    public Page<Map<String, Object>> getPage(Map<String, Object> condition) {

        IWrapper<Supplier> wrapper = IWrapper.getWrapper(condition);
        wrapper.orderByDesc(Supplier::getId)
                .keyword(new KeywordData(Supplier::getName), new KeywordData(Supplier::getContacts))
                .eq(Supplier::getType)
                .like(Supplier::getCode)
                .like(Supplier::getName)
                .eq(Supplier::getCountryId)
                .eq(Supplier::getProvinceId)
                .eq(Supplier::getCityId)
                .like(Supplier::getContacts)
                .eq(Supplier::getPhonePrefixType)
                .like(Supplier::getPhone)
                .func(q -> {
                    Integer accountPeriodType = Convert.toInt(condition.get("accountPeriodType"));
                    // 有账期
                    if (Objects.equals(accountPeriodType, AccountPeriodTypeEnum.ACCOUNTING_PERIOD.getType())) {
                        q.gt(Supplier::getAccountPeriod, 0);
                    }
                    // 无账期
                    else if (Objects.equals(accountPeriodType, AccountPeriodTypeEnum.NO_ACCOUNTING_PERIOD.getType())) {
                        q.eq(Supplier::getAccountPeriod, 0);
                    }
                })
                .func(q -> {
                    Integer returnPeriodType = Convert.toInt(condition.get("returnPeriodType"));
                    // 有退换货期限/天
                    if (Objects.equals(returnPeriodType, AccountPeriodTypeEnum.ACCOUNTING_PERIOD.getType())) {
                        q.gt(Supplier::getReturnPeriod, 0);
                    }
                    // 无退换货期限/天
                    else if (Objects.equals(returnPeriodType, AccountPeriodTypeEnum.NO_ACCOUNTING_PERIOD.getType())) {
                        q.eq(Supplier::getReturnPeriod, 0);
                    }
                });

        Page<Map<String, Object>> page = pageMaps(condition, wrapper);

        List<Map<String, Object>> records = page.getRecords();
        if (records.size() == 0) {
            return page;
        }

        // 赋值附件
        FileClientUtil.setFileInfoListMap(records);
        // 赋值国省市
        RegionClientUtil.setRegionName(records);

        return page;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SupplierVo supplierVo) {

        synchronized (this) {
            supplierVo.setCode(CodeEnum.SUPPLIER.getCode(supplierVo.getCode()));
            save(supplierVo);
        }

        // 绑定附件
        FileClientUtil.bindingFile(supplierVo.getId(), supplierVo.getFileInfoList());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SupplierVo supplierVo) {
        updateById(supplierVo);
        // 修改附件绑定信息
        FileClientUtil.againBindingFile(supplierVo.getId(), supplierVo.getFileInfoList());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(SupplierVo supplierVo) {
        removeById(supplierVo.getId());
        // 删除附件
        FileClientUtil.relieveBindingFile(supplierVo.getId());
    }

    @Override
    public List<Map<String, Object>> getList(SupplierVo supplierVo) {
        IWrapper<Supplier> wrapper = IWrapper.getWrapper(supplierVo);

        wrapper.like(Supplier::getName);
        wrapper.like(Supplier::getCode);
        wrapper.eq(Supplier::getType);
        wrapper.select(Supplier::getId, Supplier::getType, Supplier::getCode, Supplier::getName);

        return listMaps(wrapper);
    }

}
