package com.dxhy.manage.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.constant.CommonConstants;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.util.StringHelper;
import com.dxhy.common.utils.R;
import com.dxhy.manage.service.OperationsToolsService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author futianhao
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
@RequestMapping("/manage/operationsTools")
@Slf4j
public class OperationsToolsController extends AbstractController {

    @Resource
    private OperationsToolsService operationsToolsService;

    /**
     * 通过导入文件更改入库日期
     */
    @RequestMapping("/updateCreateDateByImport")
    public ResponseEntity<?> updateCreateDateByImport(@RequestBody String postInvoiceDetail) {
        try {
            JSONObject invoiceDetail = JSONObject.parseObject(postInvoiceDetail);
            if (!invoiceDetail.containsKey("zsl") || StringHelper.isBlank(invoiceDetail.getString("zsl"))) {
                return ResponseEntity.ok(R.error("修改发票数量不一致！"));
            }
            JSONArray detailArray;
            if (!invoiceDetail.containsKey("invoices")) {
                return ResponseEntity.ok(R.error("修改发票信息不能为空！"));
            } else {
                detailArray = (JSONArray)invoiceDetail.get("invoices");
                if (detailArray == null || detailArray.size() != invoiceDetail.getLongValue("zsl")) {
                    return ResponseEntity.ok(R.error("发票明细数量和总数量不一致！"));
                }
            }
            return ResponseEntity
                .ok(R.ok().put("data", operationsToolsService.updateCreateDateByInvoices(detailArray)));

        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 整个集团更新入库时间
     */
    @RequestMapping("/blocList")
    public ResponseEntity<?> blocList() {

        return ResponseEntity.ok(R.ok().put("data", operationsToolsService.getBlocList()));
    }

    /**
     * 整个集团更新入库时间
     */
    @RequestMapping("/taxnoList")
    public ResponseEntity<?> taxnoList(@RequestBody Map<String, Object> params) {
        String blocId = (String)params.get("deptId");
        return ResponseEntity.ok(R.ok().put("data", operationsToolsService.getTaxNoList(blocId)));
    }

    /**
     * 整个集团更新入库时间
     */
    @RequestMapping("/updateCreateDateByBloc")
    public ResponseEntity<?> updateCreateDateByBloc(@RequestBody Map<String, Object> params) {
        try {
            return ResponseEntity.ok(R.ok().put("data", operationsToolsService.updateCreateDateByBlocId(params)));

        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

}
