package com.dxhy.sign.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.constant.CommonConstants;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.util.StringHelper;
import com.dxhy.common.util.UserInfoUtil;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.common.utils.R;
import com.dxhy.sign.service.fpqs.YlSignService;

/**
 * @author liudan
 * @date 2020/12/7
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
public class YlSignController extends AbstractController {

    @Resource
    private YlSignService ylSignService;

    @RequestMapping("/ylqs/list")
    @ResponseBody
    @SysLog("艺龙签收成功列表查询")
    public ResponseEntity<?> ylqsList(@RequestBody Map<String, Object> pramsMap) {
        // 入参统一在入口处理
        pramsMap.put("dbName", getUserInfo().getDbName());
        List<String> gfshList = new ArrayList<>();
        if (!"99".equals(pramsMap.get("gfsh")) && pramsMap.get("gfsh") != null && !"".equals(pramsMap.get("gfsh"))) {
            gfshList.add(pramsMap.get("gfsh").toString());
        } else {
            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
            if (gfshList.size() == 0) {
                return ResponseEntity.ok(R.ok().put("data", ""));
            }
        }
        pramsMap.put("gfsh", gfshList);
        if (pramsMap.get("qsrqq") != null && !"".equals(pramsMap.get("qsrqq"))) {
            pramsMap.put("qsrqq", DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("qsrqq").toString())));
        } else {
            return ResponseEntity.ok(R.error("签收日期不能为空！"));
        }
        if (pramsMap.get("qsrqz") != null && !"".equals(pramsMap.get("qsrqz"))) {
            pramsMap.put("qsrqz", DateUtils.getNight(DateUtils.strToDate(pramsMap.get("qsrqz").toString())));
        } else {
            return ResponseEntity.ok(R.error("签收日期不能为空！"));
        }

        pramsMap.put("invoiceNo", pramsMap.get("invoiceNo"));
        int curr = (int)pramsMap.get("curr");
        int size = (int)pramsMap.get("size");

        try {
            return ResponseEntity.ok(R.ok().put("data", ylSignService.ylqsList(pramsMap, curr, size)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 批量删除
     *
     * @return 返回结果
     *
     */
    @PostMapping("/ylDel")
    @ResponseBody
    @SysLog("艺龙批量删除")
    public ResponseEntity<?> batchDel(@RequestBody String postInvoiceDetail) {
        // 入参统一在入口处理
        Map<String, String> pramsMap = new HashMap<>(8);

        JSONObject invoiceDetail = JSONObject.parseObject(postInvoiceDetail);

        if (!invoiceDetail.containsKey("uuids")) {
            return ResponseEntity.ok(R.error("删除发票信息不能为空！"));
        }
        pramsMap.put("dbName", getUserInfo().getDbName());
        pramsMap.put("uuids", postInvoiceDetail);

        try {

            return ResponseEntity.ok(R.ok().put("data", ylSignService.ylDel(pramsMap)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 批量修改签收时间
     *
     * @param postInfo
     *            前端传入的待变更信息
     *
     * @return 返回结果
     *
     */
    @PostMapping("/change/qsDate")
    @ResponseBody
    @SysLog("修改签收时间")
    public ResponseEntity<?> yqsxxChangeBusiness(@RequestBody String postInfo) {
        // 入参统一在入口处理
        Map<String, String> pramsMap = new HashMap<>(8);
        pramsMap.put("dbName", getUserInfo().getDbName());

        try {
            if (!StringHelper.isBlank(postInfo)) {
                JSONObject changeInfo = JSONObject.parseObject(postInfo);
                if (changeInfo.containsKey("uuids")) {
                    JSONArray changeArray = changeInfo.getJSONArray("uuids");
                    if (changeArray == null || changeArray.size() == 0) {
                        return ResponseEntity.ok(R.error("需要变更的记录不能为空！"));
                    } else {
                        pramsMap.put("changeInfo", postInfo);
                    }
                } else {
                    return ResponseEntity.ok(R.error("需要变更的记录不能为空！"));
                }

            } else {
                return ResponseEntity.ok(R.error("需要变更的记录不能为空！"));
            }

            String msg = ylSignService.changeQsDate(pramsMap);
            if (msg != null) {
                return ResponseEntity.ok(R.error(msg));
            } else {
                return ResponseEntity.ok(R.ok().put("data", "修改成功"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error("修改失败"));
        }
    }

}
