package com.dxhy.extend.controller;

import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.constant.CommonConstants;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.util.UserInfoUtil;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.common.utils.R;
import com.dxhy.extend.service.companypool.SaleSignService;
import com.dxhy.extend.service.companypool.TicketService;
import lombok.extern.slf4j.Slf4j;
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 javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * 销项发票签收查询
 *
 * @author dxhy
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
@RequestMapping("modules/saleSign")
@Slf4j
public class ExtSaleSignController extends AbstractController {

    @Resource
    private SaleSignService saleSignService;

    /**
     * 销项签收日志
     *
     * @param paramsMap
     *            查询条件
     * @return 返回
     */
    @RequestMapping("/querySignLog")
    @SysLog("销项签收日志查询")
    public ResponseEntity<R> queryTicket(@RequestBody Map<String, Object> paramsMap) {
        paramsMap.put("dbName", getUserInfo().getDbName());

        List<String> gfshList = new ArrayList<>();
        if (!"99".equals(paramsMap.get("gfsh")) && paramsMap.get("gfsh") != null && !"".equals(paramsMap.get("gfsh"))) {
            gfshList.add(paramsMap.get("gfsh").toString());
        } else {
            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
            if (gfshList.size() == 0) {
                return ResponseEntity.ok(R.ok().put("data", ""));
            }
        }
        paramsMap.put("gfsh", gfshList);
        if (paramsMap.get("beginDate") != null && !"".equals(paramsMap.get("beginDate"))) {
            paramsMap.put("beginDate",
                DateUtils.getMorning(DateUtils.strToDate(paramsMap.get("beginDate").toString())));
        }
        if (paramsMap.get("endDate") != null && !"".equals(paramsMap.get("endDate"))) {
            paramsMap.put("endDate", DateUtils.getNight(DateUtils.strToDate(paramsMap.get("endDate").toString())));
        }

        int curr = (int)paramsMap.get("curr");
        int size = (int)paramsMap.get("size");
        try {
            return ResponseEntity.ok(R.ok().put("data", saleSignService.queryList(paramsMap, curr, size)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 销项签收日志 - 详情
     *
     * @param paramsMap
     *            查询条件
     * @return 返回
     */
    @RequestMapping("/querySignDetailLog")
    @SysLog("销项签收日志详情查询")
    public ResponseEntity<R> queryTicketDetail(@RequestBody Map<String, Object> paramsMap) {
        paramsMap.put("dbName", getUserInfo().getDbName());

        List<String> gfshList = new ArrayList<>();
        if (!"99".equals(paramsMap.get("gfsh")) && paramsMap.get("gfsh") != null && !"".equals(paramsMap.get("gfsh"))) {
            gfshList.add(paramsMap.get("gfsh").toString());
        } else {
            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
            if (gfshList.size() == 0) {
                return ResponseEntity.ok(R.ok().put("data", ""));
            }
        }
        paramsMap.put("gfsh", gfshList);
        if (paramsMap.get("beginDate") != null && !"".equals(paramsMap.get("beginDate"))) {
            paramsMap.put("beginDate",
                DateUtils.getMorning(DateUtils.strToDate(paramsMap.get("beginDate").toString())));
        }
        if (paramsMap.get("endDate") != null && !"".equals(paramsMap.get("endDate"))) {
            paramsMap.put("endDate", DateUtils.getNight(DateUtils.strToDate(paramsMap.get("endDate").toString())));
        }

        int curr = (int)paramsMap.get("curr");
        int size = (int)paramsMap.get("size");
        try {
            return ResponseEntity.ok(R.ok().put("data", saleSignService.queryDetailList(paramsMap, curr, size)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 销项签收日志
     *
     * @param paramsMap
     *            查询条件
     * @return 返回
     */
    @RequestMapping("/querySignInvoice")
    @SysLog("销项签收日志查询")
    public ResponseEntity<R> querySignInvoice(@RequestBody Map<String, Object> paramsMap) {
        paramsMap.put("dbName", getUserInfo().getDbName());

        List<String> gfshList = new ArrayList<>();
        if (!"99".equals(paramsMap.get("gfsh")) && paramsMap.get("gfsh") != null && !"".equals(paramsMap.get("gfsh"))) {
            gfshList.add(paramsMap.get("gfsh").toString());
        } else {
            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
            if (gfshList.size() == 0) {
                return ResponseEntity.ok(R.ok().put("data", ""));
            }
        }
        paramsMap.put("gfsh", gfshList);
        if (paramsMap.get("beginDate") != null && !"".equals(paramsMap.get("beginDate"))) {
            paramsMap.put("beginDate",
                DateUtils.getMorning(DateUtils.strToDate(paramsMap.get("beginDate").toString())));
        }
        if (paramsMap.get("endDate") != null && !"".equals(paramsMap.get("endDate"))) {
            paramsMap.put("endDate", DateUtils.getNight(DateUtils.strToDate(paramsMap.get("endDate").toString())));
        }
        if (paramsMap.get("invoiceDateStart") != null && !"".equals(paramsMap.get("invoiceDateStart"))) {
            paramsMap.put("invoiceDateStart",
                DateUtils.getMorning(DateUtils.strToDate(paramsMap.get("invoiceDateStart").toString())));
        }
        if (paramsMap.get("invoiceDateEnd") != null && !"".equals(paramsMap.get("invoiceDateEnd"))) {
            paramsMap.put("invoiceDateEnd",
                DateUtils.getNight(DateUtils.strToDate(paramsMap.get("invoiceDateEnd").toString())));
        }

        int curr = (int)paramsMap.get("curr");
        int size = (int)paramsMap.get("size");
        try {
            return ResponseEntity.ok(R.ok().put("data", saleSignService.querySignInvoice(paramsMap, curr, size)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

}
