package com.sifang.modules.supply.warehouse.controller;

import com.google.common.collect.Maps;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sunshine.SystemSetting.utils.StringUtil;
import com.sifang.modules.supply.stock.utils.MaterialUtils;
import com.sifang.modules.supply.warehouse.service.CrmSupplyBarcodeService;
import com.sifang.modules.sys.common.utils.DateUtils;
import com.sifang.modules.sys.util.GuardUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseExceptionController;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping(value = "${adminPath}/barcodeManage")
public class CrmSupplyBarcodeController extends BaseExceptionController {

    @Autowired
    private CrmSupplyBarcodeService crmSupplyBarcodeService;


    @RequestMapping(value = "barcodeList")
    public String planList(Model model) {
        model.addAttribute("materialTypeList", MaterialUtils.findMaterialType());
        String minTime = DateUtils.dateChangeDays(new Date(), -6,"yyyy-MM-dd");
        model.addAttribute("maxTime", DateUtils.getDateTime());
        model.addAttribute("minTime",minTime+" 00:00:00");
        return "modules/supply/barcode/barcodeList";
    }

    @ResponseBody
    @RequestMapping(value = "barcodeDataList")
    public Object barcodeDataList(HttpServletRequest request) {
        Map<String, String> map = getParams(request);
        LayuiPage<Record> page = new LayuiPage<>(request);
        crmSupplyBarcodeService.barcodeDataList(page, map);
        return page.renderJson();
    }

    @RequestMapping(value = "toImportBarcode")
    public String toImport() {
        return "modules/supply/barcode/importBarcode";
    }

    @ResponseBody
    @RequestMapping(value = "checkImportBarcode")
    public Object checkImportBarcode(MultipartFile file) throws IOException {
        Map<String, String> params = Maps.newHashMap();
        params.put("extName", StringUtil.getFileExtName(file.getOriginalFilename()));
        Map<String, Object> retMap = crmSupplyBarcodeService.checkImportBarcode(params, file.getInputStream());
        return JSONObject.fromObject(retMap);
    }

    @RequestMapping(value = "importBarcode")
    @ResponseBody
    public Object importBarcode(MultipartFile file) throws IOException {
        Map<String, String> params = Maps.newHashMap();
        params.put("fileName", file.getOriginalFilename());
        Map<String, Object> retMap = crmSupplyBarcodeService.importBarcode(params, file.getInputStream());
        return retMap;
    }
    @ResponseBody
    @RequestMapping(value = "delete")
    public Result<Void> delete(String ids){
        GuardUtils.notBlank(ids, "ids不能为空");
        crmSupplyBarcodeService.deletePlan(ids);
        return Result.ok();
    }
}
