package com.nantian.cloud.jljc.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nantian.cloud.jljc.entity.DischargeEnergyProductMonth;
import com.nantian.cloud.jljc.entity.DischargeEnergyStockMonth;
import com.nantian.cloud.jljc.mapper.DischargeEnergyStockMonthMapper;
import com.nantian.cloud.jljc.service.DischargeEnergyProductMonthService;
import com.nantian.cloud.jljc.service.DischargeEnergyStockMonthService;
import com.nantian.cloud.jljc.tools.LpExcelUtil;
import com.nantian.cloud.jljc.vo.dischargeEnergy.DischargeEnergyStockMonthVo;
import com.nantian.cloud.jljc.vo.dischargeEnergy.DischargeEnergyTotalVo;
import com.nantian.sc.cloud.common.data.datascope.DataScope;
import com.nantian.sc.cloud.common.security.service.ScUser;
import com.nantian.sc.cloud.common.security.util.SecurityUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@Service("dischargeEnergyStockMonthService")
public class DischargeEnergyStockMonthServiceImpl extends ServiceImpl<DischargeEnergyStockMonthMapper, DischargeEnergyStockMonth> implements DischargeEnergyStockMonthService {
    @Autowired
    private DischargeEnergyStockMonthService dischargeEnergyStockMonthService;

    @Autowired
    private DischargeEnergyProductMonthService dischargeEnergyProductMonthService;

    @Override
    public Boolean addStock(String s, DischargeEnergyStockMonth dischargeEnergyStock, List<DischargeEnergyProductMonth> productList) {
        if(StringUtils.isNotEmpty(s)){
            JSONObject json1 = JSONObject.parseObject(s);
            String Product = json1.getString("dischargeEnergyProduct");
            dischargeEnergyStock = JSONObject.parseObject(json1.getString("dischargeEnergyStock"),DischargeEnergyStockMonth.class);
            productList = (List<DischargeEnergyProductMonth>) JSONArray.parseArray(Product, DischargeEnergyProductMonth.class);
        }
        ScUser u = SecurityUtils.getUser();
        dischargeEnergyStock.setDelFlag("1");
        dischargeEnergyStock.setDeptId(u.getDeptId());
        dischargeEnergyStock.setUserid(u.getId());
        Map<String, String> json = new HashMap<>();
        json.put("country", "");
        json.put("city", "");
        Map<String, String> jsonObject = new HashMap<>();
        jsonObject.put("city", "");
        jsonObject.put("province", "");
        if (u.getDeptId().toString().equals("530000")){
            dischargeEnergyStock.setAdminisFlag("");
            dischargeEnergyStock.setReportUser(JSONObject.toJSONString(json));
            json.replace("country", "1");
            json.replace("city", "1");
            dischargeEnergyStock.setReportStatus(JSONObject.toJSONString(json));
            dischargeEnergyStock.setUpdateUser(JSONObject.toJSONString(jsonObject));
            dischargeEnergyStock.setAuditUser(JSONObject.toJSONString(jsonObject));
            dischargeEnergyStock.setCause(JSONObject.toJSONString(jsonObject));
            jsonObject.replace("city", "1");
            jsonObject.replace("province", "1");
            dischargeEnergyStock.setAuditorFlag(JSONObject.toJSONString(jsonObject));
        }else if (u.getDeptId().toString().substring(u.getDeptId().toString().length() - 2).equals("00")){
            dischargeEnergyStock.setAdminisFlag("2");
            dischargeEnergyStock.setReportUser(JSONObject.toJSONString(json));
            json.replace("country", "1");
            json.replace("city", "0");
            dischargeEnergyStock.setReportStatus(JSONObject.toJSONString(json));
            dischargeEnergyStock.setUpdateUser(JSONObject.toJSONString(jsonObject));
            dischargeEnergyStock.setAuditUser(JSONObject.toJSONString(jsonObject));
            dischargeEnergyStock.setCause(JSONObject.toJSONString(jsonObject));
            jsonObject.replace("city", "1");
            jsonObject.replace("province", "0");
            dischargeEnergyStock.setAuditorFlag(JSONObject.toJSONString(jsonObject));
        }else {
            dischargeEnergyStock.setAdminisFlag("1");
            dischargeEnergyStock.setReportUser(JSONObject.toJSONString(json));
            json.replace("country", "0");
            json.replace("city", "0");
            dischargeEnergyStock.setReportStatus(JSONObject.toJSONString(json));
            dischargeEnergyStock.setUpdateUser(JSONObject.toJSONString(jsonObject));
            dischargeEnergyStock.setAuditUser(JSONObject.toJSONString(jsonObject));
            dischargeEnergyStock.setCause(JSONObject.toJSONString(jsonObject));
            jsonObject.replace("city", "0");
            jsonObject.replace("province", "0");
            dischargeEnergyStock.setAuditorFlag(JSONObject.toJSONString(jsonObject));
        }
        dischargeEnergyStockMonthService.save(dischargeEnergyStock);
        for (DischargeEnergyProductMonth r : productList) {
            r.setId(null);
            r.setUserid(u.getId());
            r.setDeptId(u.getDeptId());
            r.setItemId(dischargeEnergyStock.getId());
            r.setItemType(dischargeEnergyStock.getItemType());
            r.setDelFlag("1");
        }
        dischargeEnergyProductMonthService.saveBatch(productList);
        return null;
    }

    @Override
    public Boolean renewStockBatch(String str) {
        JSONObject json = JSONObject.parseObject(str);
        List<DischargeEnergyStockMonth> dataList = JSONArray.parseArray(json.getString("data"), DischargeEnergyStockMonth.class);
        String scYear = json.getString("scYear");
        String scMonth = json.getString("scMonth");
        for (DischargeEnergyStockMonth stockMonth : dataList) {
            stockMonth.setId(null);
            stockMonth.setYear(scYear);
            stockMonth.setQuarter(scMonth);
            addStock("", stockMonth, stockMonth.getProduceList());
        }
        return null;
    }

    public Boolean updateStock(String s) {
        JSONObject json1 = JSONObject.parseObject(s);
        String Product = json1.getString("dischargeEnergyProduct");
        DischargeEnergyStockMonth dischargeEnergyStock = JSONObject.parseObject(json1.getString("dischargeEnergyStock"),DischargeEnergyStockMonth.class);
        List<DischargeEnergyProductMonth> productList = (List<DischargeEnergyProductMonth>) JSONArray.parseArray(Product, DischargeEnergyProductMonth.class);
        dischargeEnergyStock.setDelFlag("1");
        ScUser u = SecurityUtils.getUser();
        dischargeEnergyStock.setUpdateTime(new Date());
        Map<String, String> json = new HashMap<>();
        json.put("country", "");
        json.put("city", "");
        Map<String, String> jsonObject = new HashMap<>();
        jsonObject.put("city", "");
        jsonObject.put("province", "");
        if (u.getDeptId().toString().equals("530000")){
        }else if (u.getDeptId().toString().substring(u.getDeptId().toString().length() - 2).equals("00")){
            if (dischargeEnergyStock.getDeptId().equals(u.getDeptId().toString())){
                dischargeEnergyStock.setReportUser(JSONObject.toJSONString(json));
                json.replace("country", "1");
                json.replace("city", "0");
                dischargeEnergyStock.setReportStatus(JSONObject.toJSONString(json));
                JSONObject object = JSONObject.parseObject(dischargeEnergyStock.getUpdateUser());
                object.replace("city", u.getUserRealName());
                dischargeEnergyStock.setUpdateUser(JSONObject.toJSONString(object));
                dischargeEnergyStock.setAuditUser(JSONObject.toJSONString(jsonObject));
                dischargeEnergyStock.setCause(JSONObject.toJSONString(jsonObject));
                jsonObject.replace("city", "1");
                jsonObject.replace("province", "0");
                dischargeEnergyStock.setAuditorFlag(JSONObject.toJSONString(jsonObject));
            }else {
                JSONObject object = JSONObject.parseObject(dischargeEnergyStock.getUpdateUser());
                object.replace("city", u.getUserRealName());
                dischargeEnergyStock.setUpdateUser(JSONObject.toJSONString(object));
                jsonObject.replace("city", "1");
                jsonObject.replace("province", "0");
                dischargeEnergyStock.setAuditorFlag(JSONObject.toJSONString(jsonObject));
            }
        }else {
            dischargeEnergyStock.setReportUser(JSONObject.toJSONString(json));
            json.replace("country", "0");
            json.replace("city", "0");
            dischargeEnergyStock.setReportStatus(JSONObject.toJSONString(json));
            dischargeEnergyStock.setAuditUser(JSONObject.toJSONString(jsonObject));
            dischargeEnergyStock.setCause(JSONObject.toJSONString(jsonObject));
            jsonObject.replace("city", "0");
            jsonObject.replace("province", "0");
            dischargeEnergyStock.setAuditorFlag(JSONObject.toJSONString(jsonObject));
        }
        dischargeEnergyStockMonthService.updateById(dischargeEnergyStock);

        QueryWrapper<DischargeEnergyProductMonth> queryWrapper = new QueryWrapper<DischargeEnergyProductMonth>();
        queryWrapper.eq("item_id",dischargeEnergyStock.getId());
        List<DischargeEnergyProductMonth> oldProductList=dischargeEnergyProductMonthService.list(queryWrapper);
        if(oldProductList.size()>0){
            oldProductList.forEach(r->{
                r.setDelFlag("4");
            });
            dischargeEnergyProductMonthService.updateBatchById(oldProductList);
        }
        if (productList.size()>0){
            productList.forEach(r->{
                r.setItemId(dischargeEnergyStock.getId());
                r.setItemType(dischargeEnergyStock.getItemType());
                r.setDelFlag("1");
            });
            dischargeEnergyProductMonthService.saveOrUpdateBatch(productList);
        }
        return null;
    }

    @Override
    public Boolean delStock(DischargeEnergyStockMonth dischargeEnergyStock) {
        QueryWrapper<DischargeEnergyProductMonth> queryWrapper = new QueryWrapper<DischargeEnergyProductMonth>();
        dischargeEnergyStock.setDelFlag("4");
        dischargeEnergyStockMonthService.updateById(dischargeEnergyStock);
        queryWrapper.eq("item_id",dischargeEnergyStock.getId());
        queryWrapper.eq("del_flag","1");
        queryWrapper.eq("item_type",dischargeEnergyStock.getItemType());
        List<DischargeEnergyProductMonth> productList=dischargeEnergyProductMonthService.list(queryWrapper);
        if(productList.size()>0){
            productList.forEach(r->{
                r.setItemId(dischargeEnergyStock.getId());
                r.setItemType(dischargeEnergyStock.getItemType());
                r.setDelFlag("4");
            });
            dischargeEnergyProductMonthService.updateBatchById(productList);
        }
        return null;
    }

    @Override
    public List<DischargeEnergyStockMonth> excelImport(MultipartFile file,String type) throws Exception {
        try {
            return LpExcelUtil.StockExcelToList(file,type);
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public List<DischargeEnergyTotalVo> getStockTotal(DischargeEnergyStockMonth stockMonth) {
        if (StringUtils.isBlank(stockMonth.getYear()) || StringUtils.isBlank(stockMonth.getQuarter()) || StringUtils.isBlank(stockMonth.getItemType())) {
            return null; //必须传年份、月度和项目类型
        }
        List<DischargeEnergyTotalVo> totalList = new ArrayList<>();
        String deptId = SecurityUtils.getUser().getDeptId().toString();
        if("530000".equals(deptId)){ //省级
            totalList = baseMapper.getTotal(stockMonth,deptId.substring(0,2));
            List<DischargeEnergyTotalVo> tmp = baseMapper.getTotalProv(stockMonth,deptId.substring(0,2));
            totalList.addAll(tmp);
        }else{ //州市级
            totalList = baseMapper.getTotal(stockMonth,deptId.substring(0,4));
        }
        return totalList;
    }


    @Override
    public IPage<DischargeEnergyStockMonth> getPageData(Page page, DischargeEnergyStockMonth dischargeEnergyStockMonth) {
        IPage<DischargeEnergyStockMonth> pageData = baseMapper.getPageData(page, dischargeEnergyStockMonth, new DataScope());
        return pageData;
    }

    @Override
    public IPage<DischargeEnergyStockMonth> getStockProductIndex(Page page, DischargeEnergyStockMonth dischargeEnergyStockMonth) {
        return baseMapper.getStockProductIndex(page,dischargeEnergyStockMonth, new DataScope());
    }

    @Override
    public List<Map<String, String>> getChanges(DischargeEnergyStockMonth dischargeEnergyStockMonth) {
        return baseMapper.getChanges(dischargeEnergyStockMonth);
    }

    @Override
    public DischargeEnergyStockMonth getItemChange(DischargeEnergyStockMonth item) {
        return baseMapper.getItemChange(item);
    }

    @Override
    public List<Map<String, String>> getSummaryTable1(String year, String quarter) {
        return baseMapper.getSummaryTable1(year,quarter);
    }

    @Override
    public List<Map<String, String>> getSummaryTableData(String year, String quarter, String s) {
        return baseMapper.getSummaryTableData(year,quarter,s);
    }

    @Override
    public List<Map<String, String>> getSummaryTableSum(String year, String quarter) {
        return baseMapper.getSummaryTableSum(year,quarter);
    }

    @Override
    public List<Map<String, String>> getSummaryTableDataSum(String year, String quarter, String r) {
        return baseMapper.getSummaryTableDataSum(year,quarter,r);
    }

    @Override
    public List<Map<String, String>> getSummaryTable2(String year, String quarter) {
        return baseMapper.getSummaryTableData2(year,quarter);
    }
}



