package com.zfire.mall.manager.logic.manage.take;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.admin.AdminUserCom;
import com.zfire.mall.manager.bean.k3.K3CustomerBean;
import com.zfire.mall.manager.bean.manage.take.TakeCarRecordBean;
import com.zfire.mall.manager.commonmapper.manage.take.TakeCarRecordReserveMapper;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.logic.k3.K3CustomerLogic;
import com.zfire.mall.manager.plus.entity.TakeCarRecord;
import com.zfire.mall.manager.plus.service.TakeCarRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class TakeCarRecordLogic {

    @Resource
    TakeCarRecordReserveMapper takeCarRecordReserveMapper;
    @Resource
    TakeCarRecordService takeCarRecordService;
    @Autowired
    K3CustomerLogic k3CustomerLogic;
    @Resource
    CommonLogic commonLogic;

    public IPage<TakeCarRecordBean> listPage(String customerName, String carBrand, Integer pageNum, Integer pageSize) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        List<String> adminCompanyIds = adminUser.getAdminCompanyIds();
        String customerId = null;
        if (adminUser.getIsCustomer()) {
            customerId = adminUser.getCustomerId();
        }

        return takeCarRecordReserveMapper.listPage(new Page<>(pageNum, pageSize), adminCompanyIds, customerId, customerName, carBrand);
    }

    public ExcelData export(List<TakeCarRecordBean> records) {
        String[] titles = new String[]{"经销商名称","车牌","联系电话","车型", "体型"};
        List<List<Object>> rows = new ArrayList<>();
        for (TakeCarRecordBean takeCarRecordBean : records) {
            List<Object> row = new ArrayList<>();
            row.add(takeCarRecordBean.getCustomerName());
            row.add(takeCarRecordBean.getCarBrand());
            row.add(takeCarRecordBean.getMobile());
            row.add(takeCarRecordBean.getCarType());
            row.add(takeCarRecordBean.getVolume());
            rows.add(row);
        }
        ExcelData excelData = new ExcelData();
        excelData.setRows(rows);
        excelData.setTitles(Arrays.asList(titles));
        return excelData;
    }

    @Transactional(rollbackFor = Exception.class)
    public void add(TakeCarRecordBean takeCarRecordBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        takeCarRecordBean.setAdminCompanyId(adminUser.getAdminCompanyId());
        TakeCarRecord takerRecord = new TakeCarRecord();
        BeanUtils.copyProperties(takeCarRecordBean, takerRecord);
        takerRecord.setCreateName(adminUser.getNickName());
        takerRecord.setUpdateName(adminUser.getNickName());
        takerRecord.insert();
    }

    @Transactional(rollbackFor = Exception.class)
    public void edit(TakeCarRecordBean takeCarRecordBean) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        takeCarRecordBean.setAdminCompanyId(adminUser.getAdminCompanyId());
        TakeCarRecord takerRecord = new TakeCarRecord();
        BeanUtils.copyProperties(takeCarRecordBean, takerRecord);
        takerRecord.setUpdateName(adminUser.getNickName());

        takerRecord.updateById();
    }

    @Transactional(rollbackFor = Exception.class)
    public void del(List<String> ids) {
        AdminUserCom adminUser = commonLogic.getAdminUser();
        takeCarRecordService.lambdaUpdate().in(TakeCarRecord::getAdminCompanyId, adminUser.getAdminCompanyIds())
                .in(TakeCarRecord::getId, ids.stream().distinct().collect(Collectors.toList()))
                .remove();
    }

//    public String downloadFilePath() {
//        AdminUser user = adminUserService.getById(CommonUtils.getUserId());
//        if (user.getIsCustomer()) {
//            return "/template/经销商提货车辆导入模板.xlsx";
//        }
//        return "/template/提货车辆导入模板.xlsx";
//    }
}
