package org.springblade.imports.controller;


import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.BladeUser;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.imports.dto.FctStandingBookAgainDTO;
import org.springblade.imports.dto.FctStandingBookDTO;
import org.springblade.imports.entity.FctStandingBook;
import org.springblade.imports.entity.FctStandingBookAgain;
import org.springblade.imports.entity.ImpUnicomArpuDou;
import org.springblade.imports.listener.FctStandingBookAgainExcelListener;
import org.springblade.imports.listener.FctStandingBookExcelListener;
import org.springblade.imports.query.FctStandingBookAgainQuery;
import org.springblade.imports.query.FctStandingBookQuery;
import org.springblade.imports.service.IFctStandingBookAgainService;
import org.springblade.imports.service.IVRecentVippoolCostsService;
import org.springblade.imports.util.RandomUtil;
import org.springblade.imports.vo.FctStandingBookAgainVO;
import org.springblade.imports.vo.FctStandingBookVO;
import org.springblade.system.client.UserClient;
import org.springblade.system.client.model.UserModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.InputStream;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zbc
 * @since 2023-03-13
 */
@RestController
@Slf4j
@RequestMapping("imports/fctAgain")
@Api(value = "会员进销存台账", tags = "会员进销存台账接口")
public class FctStandingBookAgainController {

    @Autowired
    private IFctStandingBookAgainService fctStandingBookService;

    @Autowired
    private IVRecentVippoolCostsService recentVippoolCostsService;

    @Autowired
    private UserClient userService;


    @GetMapping("/page")
    @ApiOperation(value = "会员进销存台账分页", notes = "传入pageQuery")
    public R list(FctStandingBookAgainQuery pageQuery, Query query) {
        IPage<FctStandingBookAgain> pages = fctStandingBookService.getPageByQuery(Condition.getPage(query), pageQuery);
        return R.data(pages);
    }


    @PostMapping("/insert")
    @ApiOperation(value = "单条数据插入", notes = "传入fctStandingBook")
    public R insert(@Valid @RequestBody FctStandingBookAgain fctStandingBook) {
        if (fctStandingBook == null || fctStandingBook.getOnDid() == null) {
            return R.fail("参数异常");
        }
        BladeUser bladeUser = AuthUtil.getUser();
        UserModel user = userService.getById(bladeUser.getUserId());
        fctStandingBook.setAccountId(user.getAccount());
        fctStandingBook.setAccountName(user.getName());
        fctStandingBook.setImportTime(new Date());
        List<FctStandingBookAgain> updateList = fctStandingBookService.list(new LambdaQueryWrapper<FctStandingBookAgain>().gt(FctStandingBookAgain::getDId, fctStandingBook.getOnDid()));
        if(!updateList.isEmpty()&&updateList.size()>0){
            List<FctStandingBookAgain> collect = updateList.stream().sorted((o1, o2) -> o2.getDId().intValue() - o1.getDId().intValue()).collect(Collectors.toList());
            for (FctStandingBookAgain fct : collect) {
                LambdaQueryWrapper<FctStandingBookAgain> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(FctStandingBookAgain::getDId,fct.getDId());
                fct.setDId(fct.getDId() + 1);
                fctStandingBookService.update(fct,queryWrapper);
            }
        }
        fctStandingBook.setDId(fctStandingBook.getOnDid()+1);
        return R.data(fctStandingBookService.save(fctStandingBook));
    }


    @GetMapping("/select/list")
    @ApiOperation(value = "进销存下拉列表")
    public R selectList() {
        List<FctStandingBookAgain> list = fctStandingBookService.list();
        List<Map<String, Object>> data = list.stream()
                .filter(obj -> StringUtils.isNotBlank(obj.getSpname()))
                .collect(
                        Collectors.collectingAndThen(
                                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(FctStandingBookAgain::getSpname))), ArrayList::new)
                ).stream()
                .map(obj -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("label", obj.getSpname());
                    map.put("value", obj.getSpname());
                    return map;
                }).collect(Collectors.toList());
        List<Map<String, Object>> data2 = list.stream()
                .filter(obj -> StringUtils.isNotBlank(obj.getOutChannel()))
                .collect(
                        Collectors.collectingAndThen(
                                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(FctStandingBookAgain::getOutChannel))), ArrayList::new)
                ).stream()
                .map(obj -> {
                    Map<String, Object> map = new HashMap<>();
                    map.put("label", obj.getOutChannel());
                    map.put("value", obj.getOutChannel());
                    return map;
                }).collect(Collectors.toList());
        Map<String, Object> maps = new HashMap<>();
        maps.put("list1",data);
        maps.put("list2",data2);
        return R.data(maps);
    }


    @GetMapping("/supplierList")
    @ApiOperation(value = "一级列表 - 供货商下拉列表")
    public R supplierList() {
        List<String> list = recentVippoolCostsService.supplierList();
        return R.data(list);
    }


    @GetMapping("/ptypeList")
    @ApiOperation(value = "二级列表 - 产品类型下拉列表")
    public R ptypeList(String supplier) {
        if(StringUtils.isBlank(supplier)){
            return R.fail("供货商不能为空");
        }
        List<String> list = recentVippoolCostsService.ptypeList(supplier);
        return R.data(list);
    }


    @GetMapping("/contractIdList")
    @ApiOperation(value = "二级列表 - 合同编号下拉列表")
    public R contractIdList(String supplier) {
        if(StringUtils.isBlank(supplier)){
            return R.fail("供货商不能为空");
        }
        List<String> list = recentVippoolCostsService.contractIdList(supplier);
        return R.data(list);
    }


    @GetMapping("/qytypeList")
    @ApiOperation(value = "三级列表 - 权益类型下拉列表")
    public R qytypeList(String supplier,String ptype) {
        if(StringUtils.isBlank(supplier)){
            return R.fail("供货商不能为空");
        }
        if(StringUtils.isBlank(ptype)){
            return R.fail("权益类型不能为空");
        }
        List<String> list = recentVippoolCostsService.qytypeList(supplier,ptype);
        return R.data(list);
    }


    @GetMapping("/cpIdList")
    @ApiOperation(value = "四级列表 - 供货商侧标识下拉列表")
    public R cpIdList(String supplier,String ptype,String qytype) {
        if(StringUtils.isBlank(supplier)){
            return R.fail("供货商不能为空");
        }
        if(StringUtils.isBlank(ptype)){
            return R.fail("权益类型不能为空");
        }
        if(StringUtils.isBlank(qytype)){
            return R.fail("产品类型不能为空");
        }
        List<String> list = recentVippoolCostsService.cpIdList(supplier,ptype,qytype);
        return R.data(list);
    }


    @PostMapping("/remove")
    @ApiOperation(value = "删除", notes = "传入DID")
    public R remove(@ApiParam(value = "主键DID", required = true) @RequestParam Long dId) {
        return R.status(fctStandingBookService.remove(new LambdaQueryWrapper<FctStandingBookAgain>()
                .eq(FctStandingBookAgain::getDId, dId)
        ));
    }


    @PostMapping("/update")
    @ApiOperation(value = "修改", notes = "传入fctStandingBook")
    public R update(@Valid @RequestBody FctStandingBookAgain fctStandingBook) {
        if (fctStandingBook == null || fctStandingBook.getDId() == null) {
            return R.fail("参数异常");
        }
        return R.status(fctStandingBookService.update(fctStandingBook, new LambdaQueryWrapper<FctStandingBookAgain>()
                .eq(FctStandingBookAgain::getDId, fctStandingBook.getDId()
                )
        ));
    }


    @PostMapping("/jumpQueue")
    @ApiOperation(value = "会员进销存excel导入插入数据", notes = "传入file,onDid")
    public R saveOne(@RequestParam(required = true, name = "file") MultipartFile file,
                     @RequestParam(required = true, name = "onDid") Long onDid
    ) {
        if (file == null) {
            return R.fail("导入文件不能为空");
        }
        FctStandingBookAgainExcelListener fctStandingBookExcelListener = new FctStandingBookAgainExcelListener();
        List<FctStandingBookAgainDTO> excelList = fctStandingBookExcelListener.list;
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, FctStandingBookAgainDTO.class, fctStandingBookExcelListener).sheet().doRead();
            log.info("导入条数" + excelList.size());
            if(excelList.isEmpty() || excelList.size() < 1){
                return R.fail("没有可导入的数据");
            }

            //5个下拉选项数据校验
            for (FctStandingBookAgainDTO item : excelList) {
                String supplier = item.getSupplier();
                //产品
                String spname = item.getSpname();
                //权益
                String ritype = item.getRitype();
                String contractid = item.getContractid();
                String supplierid = item.getSupplierid();
                if(StringUtils.isBlank(supplier) || StringUtils.isBlank(spname) || StringUtils.isBlank(ritype) || StringUtils.isBlank(supplierid)){
                    return R.fail("必填字段不能为空");
                }
                //供应商列表
                List<String> supplierlist = recentVippoolCostsService.supplierList();
                if(!supplierlist.contains(supplier)){
                    return R.fail("您上传的文件中：供货商，产品类型，权益类型，合同ID，供应商侧ID,不在校验字典中，请清洗数据后在上传");
                }
                //产品类型列表
                List<String> ptypeList = recentVippoolCostsService.ptypeList(supplier);
                if(!ptypeList.contains(spname)){
                    return R.fail("您上传的文件中：供货商，产品类型，权益类型，合同ID，供应商侧ID,不在校验字典中，请清洗数据后在上传");
                }
                //合同ID列表
                /*List<String> contractIdList = recentVippoolCostsService.contractIdList(supplier);
                if(!contractIdList.contains(contractid)){
                    return R.fail("您上传的文件中：供货商，产品类型，权益类型，合同ID，供应商侧ID,不在校验字典中，请清洗数据后在上传");
                }*/
                //权益列表
                List<String> qytypeList = recentVippoolCostsService.qytypeList(supplier,spname);
                if(!qytypeList.contains(ritype)){
                    return R.fail("您上传的文件中：供货商，产品类型，权益类型，合同ID，供应商侧ID,不在校验字典中，请清洗数据后在上传");
                }
                //供应商侧列表
                List<String> cpIdList = recentVippoolCostsService.cpIdList(supplier,spname,ritype);
                if(!cpIdList.contains(supplierid)){
                    return R.fail("您上传的文件中：供货商，产品类型，权益类型，合同ID，供应商侧ID,不在校验字典中，请清洗数据后在上传");
                }
            }

            Long did = onDid;
            for (FctStandingBookAgainDTO fctStandingBookDTO : excelList) {
                ++did;
                fctStandingBookDTO.setDId(did);
            }
            BladeUser bladeUser = AuthUtil.getUser();
            UserModel user = userService.getById(bladeUser.getUserId());

            List<FctStandingBookAgain> list = excelList.stream()
                    .map(obj -> {
                        FctStandingBookAgain bean = new FctStandingBookAgain();
                        bean.setAccountId(user.getAccount());
                        bean.setAccountName(user.getName());
                        bean.setImportTime(new Date());
                        BeanUtils.copyProperties(obj, bean);
                        //结算日期处理
                        if (StringUtils.isNotBlank(obj.getThemonth())) {
                            String replace = obj.getThemonth().replace("年", "-").replace("月", "");
                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm");
                            Date parse = null;
                            try {
                                parse = simpleDateFormat.parse(replace);
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            String format = simpleDateFormat.format(parse);
                            bean.setThemonth(format);
                        }
                        //入库时间处理
                        if(StringUtils.isNotBlank(obj.getInTime())){
                            String replace = obj.getThemonth().replaceAll("[^\\d]","");
                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyymmdd");
                            Date parse = null;
                            try {
                                parse = simpleDateFormat.parse(replace);
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            bean.setInTime(parse);
                        }

                        //采购金额
                        if (StringUtils.isNotBlank(obj.getInMoney())) {
                            bean.setInMoney(new BigDecimal(obj.getInMoney().replaceAll(",", "")).setScale(2, BigDecimal.ROUND_HALF_UP));
                        }
                        //采购数量
                        if (StringUtils.isNotBlank(obj.getInNum())) {
                            bean.setInNum(Double.parseDouble(obj.getInNum()));
                        }
                        //采购赠卡数量
                        if (StringUtils.isNotBlank(obj.getInGiftcardNum())) {
                            bean.setInGiftcardNum(Double.parseDouble(obj.getInGiftcardNum()));
                        }
                        //采购单价
                        if (StringUtils.isNotBlank(obj.getInPrice())) {
                            bean.setInPrice(new BigDecimal(obj.getInPrice().replaceAll(",", "")).setScale(2, BigDecimal.ROUND_HALF_UP));
                        }
                        //出库单价
                        if (StringUtils.isNotBlank(obj.getOutPrice())) {
                            bean.setOutPrice(new BigDecimal(obj.getOutPrice().replaceAll(",", "")).setScale(2, BigDecimal.ROUND_HALF_UP));
                        }
                        //出库金额
                        if (StringUtils.isNotBlank(obj.getOutMoney())) {
                            bean.setOutMoney(new BigDecimal(obj.getOutMoney().replaceAll(",", "")).setScale(2, BigDecimal.ROUND_HALF_UP));
                        }
                        //销售单价
                        if (StringUtils.isNotBlank(obj.getSalePrice())) {
                            bean.setSalePrice(new BigDecimal(obj.getSalePrice().replaceAll(",", "")).setScale(2, BigDecimal.ROUND_HALF_UP));
                        }
                        //实际结算数量
                        if (StringUtils.isNotBlank(obj.getAccountNum())) {
                            bean.setAccountNum(Double.parseDouble(obj.getAccountNum()));
                        }
                        //销售赠卡数量
                        if (StringUtils.isNotBlank(obj.getSaleGiftcardNum())) {
                            bean.setSaleGiftcardNum(Double.parseDouble(obj.getSaleGiftcardNum()));
                        }
                        //实际结算金额
                        if (StringUtils.isNotBlank(obj.getAccountMoney())) {
                            bean.setAccountMoney(new BigDecimal(obj.getAccountMoney().replaceAll(",", "")).setScale(2, BigDecimal.ROUND_HALF_UP));
                        }
                        //系统出库数量
                        if(StringUtils.isNotBlank(obj.getSystemNum())){
                            bean.setSystemNum(Double.parseDouble(obj.getSystemNum()));
                        }
                        //结余金额
                        if (StringUtils.isNotBlank(obj.getBalanceMoney())) {
                            bean.setBalanceMoney(new BigDecimal(obj.getBalanceMoney().replaceAll(",", "")).setScale(2, BigDecimal.ROUND_HALF_UP));
                        }
                        //结余正价卡数量
                        /*if (StringUtils.isNotBlank(obj.getBalanceNormalCard())) {
                            bean.setBalanceNormalCard(Double.parseDouble(obj.getBalanceNormalCard()));
                        }*/
                        //结余赠卡数量
                        /*if (StringUtils.isNotBlank(obj.getBalanceGiftcard())) {
                            bean.setBalanceGiftcard(Double.parseDouble(obj.getBalanceGiftcard()));
                        }*/
                        return bean;
                    }).collect(Collectors.toList());

            List<FctStandingBookAgain> updateList = fctStandingBookService.list(new LambdaQueryWrapper<FctStandingBookAgain>().gt(FctStandingBookAgain::getDId, onDid));
            if(!updateList.isEmpty()&&updateList.size()>0){
                List<FctStandingBookAgain> collect = updateList.stream().sorted((o1, o2) -> o2.getDId().intValue() - o1.getDId().intValue()).collect(Collectors.toList());
                for (FctStandingBookAgain fctStandingBook : collect) {
                    LambdaQueryWrapper<FctStandingBookAgain> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq(FctStandingBookAgain::getDId,fctStandingBook.getDId());
                    fctStandingBook.setDId(fctStandingBook.getDId() + excelList.size());
                    fctStandingBookService.update(fctStandingBook,queryWrapper);
                }
            }
            fctStandingBookService.saveBatch(list);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("插入失败");
        }
        return R.success("插入成功");
    }


    @PostMapping("/importFctStandingBookAgain")
    @ApiOperation(value = "会员进销存excel导入", notes = "file")
    public R importFctStandingBookAgain(@RequestParam(required = false,name = "file") MultipartFile file) {
        if (file.isEmpty()) {
            return R.fail("导入文件不能为空");
        }
        FctStandingBookAgainExcelListener fctStandingBookExcelListener = new FctStandingBookAgainExcelListener();
        List<FctStandingBookAgainDTO> excelList = fctStandingBookExcelListener.list;
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, FctStandingBookAgainDTO.class, fctStandingBookExcelListener).sheet().doRead();
            log.info("导入条数" + excelList.size());

            //5个下拉选项数据校验
            for (FctStandingBookAgainDTO item : excelList) {
                String supplier = item.getSupplier();
                //产品
                String spname = item.getSpname();
                //权益
                String ritype = item.getRitype();
                String contractid = item.getContractid();
                String supplierid = item.getSupplierid();
                if(StringUtils.isBlank(supplier) || StringUtils.isBlank(spname) || StringUtils.isBlank(ritype) || StringUtils.isBlank(supplierid)){
                    return R.fail("必填字段不能为空");
                }
                //供应商列表
                List<String> supplierlist = recentVippoolCostsService.supplierList();
                if(!supplierlist.contains(supplier)){
                    return R.fail("您上传的文件中：供货商，产品类型，权益类型，合同ID，供应商侧ID,不在校验字典中，请清洗数据后在上传");
                }
                //产品类型列表
                List<String> ptypeList = recentVippoolCostsService.ptypeList(supplier);
                if(!ptypeList.contains(spname)){
                    return R.fail("您上传的文件中：供货商，产品类型，权益类型，合同ID，供应商侧ID,不在校验字典中，请清洗数据后在上传");
                }
                //合同ID列表
               /* List<String> contractIdList = recentVippoolCostsService.contractIdList(supplier);
                if(!contractIdList.contains(contractid)){
                    return R.fail("您上传的文件中：供货商，产品类型，权益类型，合同ID，供应商侧ID,不在校验字典中，请清洗数据后在上传");
                }*/
                //权益列表
                List<String> qytypeList = recentVippoolCostsService.qytypeList(supplier,spname);
                if(!qytypeList.contains(ritype)){
                    return R.fail("您上传的文件中：供货商，产品类型，权益类型，合同ID，供应商侧ID,不在校验字典中，请清洗数据后在上传");
                }
                //供应商侧列表
                List<String> cpIdList = recentVippoolCostsService.cpIdList(supplier,spname,ritype);
                if(!cpIdList.contains(supplierid)){
                    return R.fail("您上传的文件中：供货商，产品类型，权益类型，合同ID，供应商侧ID,不在校验字典中，请清洗数据后在上传");
                }
            }


            Long did = fctStandingBookService.getDid();
            if(did == null){
                did = 1L;
            }
            for (FctStandingBookAgainDTO fctStandingBookDTO : excelList) {
                ++did;
                fctStandingBookDTO.setDId(did);
            }

            BladeUser bladeUser = AuthUtil.getUser();
            UserModel user = userService.getById(bladeUser.getUserId());
            List<FctStandingBookAgain> list = excelList.stream()
                    .map(obj -> {
                        FctStandingBookAgain bean = new FctStandingBookAgain();
                        bean.setAccountId(user.getAccount());
                        bean.setAccountName(user.getName());
                        bean.setImportTime(new Date());
                        BeanUtils.copyProperties(obj,bean);
                        //结算日期处理
                        if(StringUtils.isNotBlank(obj.getThemonth())){
                            String replace = obj.getThemonth().replace("年", "-").replace("月", "");
                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm");
                            Date parse = null;
                            try {
                                parse = simpleDateFormat.parse(replace);
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            String format = simpleDateFormat.format(parse);
                            bean.setThemonth(format);
                        }
                        //入库时间处理
                        if(StringUtils.isNotBlank(obj.getInTime())){
                            String replace = obj.getThemonth().replaceAll("[^\\d]","");
                            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyymmdd");
                            Date parse = null;
                            try {
                                parse = simpleDateFormat.parse(replace);
                            } catch (ParseException e) {
                                e.printStackTrace();
                            }
                            bean.setInTime(parse);
                        }
                        //采购金额
                        if(StringUtils.isNotBlank(obj.getInMoney())){
                            bean.setInMoney(new BigDecimal(obj.getInMoney().replaceAll(",", "")).setScale(2, BigDecimal.ROUND_HALF_UP));
                        }
                        //采购数量
                        if(StringUtils.isNotBlank(obj.getInNum())){
                            bean.setInNum(Double.parseDouble(obj.getInNum()));
                        }
                        //采购赠卡数量
                        if(StringUtils.isNotBlank(obj.getInGiftcardNum())){
                            bean.setInGiftcardNum(Double.parseDouble(obj.getInGiftcardNum()));
                        }
                        //采购单价
                        if(StringUtils.isNotBlank(obj.getInPrice())){
                            bean.setInPrice(new BigDecimal(obj.getInPrice().replaceAll(",", "")).setScale(2, BigDecimal.ROUND_HALF_UP));
                        }
                        //出库单价
                        if(StringUtils.isNotBlank(obj.getOutPrice())){
                            bean.setOutPrice(new BigDecimal(obj.getOutPrice().replaceAll(",", "")).setScale(2, BigDecimal.ROUND_HALF_UP));
                        }
                        //出库金额
                        if (StringUtils.isNotBlank(obj.getOutMoney())) {
                            bean.setOutMoney(new BigDecimal(obj.getOutMoney().replaceAll(",", "")).setScale(2, BigDecimal.ROUND_HALF_UP));
                        }
                        //销售单价
                        if(StringUtils.isNotBlank(obj.getSalePrice())){
                            bean.setSalePrice(new BigDecimal(obj.getSalePrice().replaceAll(",", "")).setScale(2, BigDecimal.ROUND_HALF_UP));
                        }
                        //实际结算数量
                        if(StringUtils.isNotBlank(obj.getAccountNum())){
                            bean.setAccountNum(Double.parseDouble(obj.getAccountNum()));
                        }
                        //销售赠卡数量
                        if(StringUtils.isNotBlank(obj.getSaleGiftcardNum())){
                            bean.setSaleGiftcardNum(Double.parseDouble(obj.getSaleGiftcardNum()));
                        }
                        //实际结算金额
                        if(StringUtils.isNotBlank(obj.getAccountMoney())){
                            bean.setAccountMoney(new BigDecimal(obj.getAccountMoney().replaceAll("\\(","").replaceAll("\\)","").replaceAll(",", "")).setScale(2, BigDecimal.ROUND_HALF_UP));
                        }
                        //系统出库数量
                        if(StringUtils.isNotBlank(obj.getSystemNum())){
                            bean.setSystemNum(Double.parseDouble(obj.getSystemNum()));
                        }
                        //结余金额
                        if(StringUtils.isNotBlank(obj.getBalanceMoney())){
                            bean.setBalanceMoney(new BigDecimal(obj.getBalanceMoney().replaceAll(",", "")).setScale(2, BigDecimal.ROUND_HALF_UP));
                        }
                        //结余正价卡数量
                        /*if(StringUtils.isNotBlank(obj.getBalanceNormalCard())){
                            bean.setBalanceNormalCard(Double.parseDouble(obj.getBalanceNormalCard()));
                        }*/
                        //结余赠卡数量
                        /*if(StringUtils.isNotBlank(obj.getBalanceGiftcard())){
                            bean.setBalanceGiftcard(Double.parseDouble(obj.getBalanceGiftcard()));
                        }*/
                        return bean;
                    }).collect(Collectors.toList());
            fctStandingBookService.saveBatch(list);
        }catch (Exception e){
            e.printStackTrace();
            return R.fail("导入失败");
        }
        return R.success("导入成功");
    }


    @Value("${blade.file.uploadPath}")
    private String picDir;


    @PostMapping("/exportFctStandingBookAgain")
    @ApiOperation(value = "会员进销存excel导出", notes = "query")
    public R exportFctStandingBookAgain(HttpServletResponse response, FctStandingBookAgainQuery query) {
        List<FctStandingBookAgain> list = fctStandingBookService.getListByQuery(query);
        String tempFileName = UUID.randomUUID().toString() + RandomUtil.randomString(4) + ".xls";
        List<FctStandingBookAgainVO> voList = new ArrayList<FctStandingBookAgainVO>();
        if(list != null && !list.isEmpty()) {
            list.forEach(item -> voList.add(BeanUtil.copyProperties(item, FctStandingBookAgainVO.class)));
        }
        try {
            String filePath = picDir + tempFileName;
            EasyExcel.write(filePath, FctStandingBookAgainVO.class).sheet().doWrite(voList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.success("uploads/" + tempFileName);
    }


}

