CustomerController.java 12.6 KB
Newer Older
eddie.woo's avatar
eddie.woo committed
1 2
package pwc.taxtech.atms.controller;

3
import com.alibaba.fastjson.JSON;
4 5
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
eddie.woo's avatar
eddie.woo committed
6 7 8 9
import org.nutz.lang.Lang;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
10 11
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
eddie.woo's avatar
eddie.woo committed
12 13 14 15 16 17 18
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
19
import org.thymeleaf.util.StringUtils;
20
import pwc.taxtech.atms.dpo.PagingDto;
eddie.woo's avatar
eddie.woo committed
21
import pwc.taxtech.atms.dto.OperationResultDto;
22
import pwc.taxtech.atms.dto.PagingResultDto;
eddie.woo's avatar
eddie.woo committed
23 24
import pwc.taxtech.atms.dto.customer.CustomerDto;
import pwc.taxtech.atms.dto.customer.CustomerValidateInfoDto;
25 26 27
import pwc.taxtech.atms.dto.vatdto.CustomsInvoiceDto;
import pwc.taxtech.atms.dto.vatdto.CustomsInvoiceFilter;
import pwc.taxtech.atms.dto.vatdto.JsonCustomsInvoice;
eddie.woo's avatar
eddie.woo committed
28 29
import pwc.taxtech.atms.service.CustomerService;

30 31 32 33 34 35 36
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

37 38
/**
 */
eddie.woo's avatar
eddie.woo committed
39 40 41 42 43 44 45 46 47 48 49 50 51
@RestController
@RequestMapping("/api/v1/customer")
public class CustomerController {

    private static final Logger logger = LoggerFactory.getLogger(CustomerController.class);

    @Autowired
    private CustomerService customerService;

    @ApiOperation(value = "财务数据客户增加")
    // @ApiImplicitParam(name = "customerDtoList", value = "customerDto List",
    // required = true, dataType = "List<CustomerDto>")
    @RequestMapping(value = "/AddRange", method = RequestMethod.POST)
52 53
    public @ResponseBody
    List<OperationResultDto<CustomerDto>> addRange(
eddie.woo's avatar
eddie.woo committed
54 55 56 57 58 59
            @RequestBody List<CustomerDto> customerDtoList) {
        logger.debug("CustomerController /AddRange");
        return customerService.addRange(customerDtoList);
    }

    @ApiOperation(value = "财务数据账套下客户查询")
60
    @ApiImplicitParam(name = "id", value = "记录Id", required = true, dataType = "java.lang.String")
frank.xa.zhang's avatar
frank.xa.zhang committed
61
    @RequestMapping(value = "/getByID", method = RequestMethod.GET)
62
    public @ResponseBody
frank.xa.zhang's avatar
frank.xa.zhang committed
63 64
    CustomerValidateInfoDto getById(@RequestParam(name = "ID") String setId) {
        logger.debug("enter getByID");
65
        return customerService.getByEnterpriseAccountSetId(setId);
eddie.woo's avatar
eddie.woo committed
66 67 68 69
    }

    @ApiOperation(value = "财务数据所有客户查询")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
70 71
    public @ResponseBody
    List<CustomerDto> getCustomer() {
eddie.woo's avatar
eddie.woo committed
72 73 74 75 76 77 78 79
        logger.debug("enter getCustomer");
        return customerService.getCustomer();
    }

    @ApiOperation(value = "财务数据客户删除")
    // @ApiImplicitParam(name = "customerDtoList", value = "customerDto List",
    // required = true, dataType = "List<CustomerDto>")
    @RequestMapping(value = "/DeleteRange", method = RequestMethod.POST)
80 81
    public @ResponseBody
    OperationResultDto<String> deleteRange(@RequestBody List<CustomerDto> customerDtoList) {
eddie.woo's avatar
eddie.woo committed
82 83 84 85 86 87 88 89
        logger.debug("enter deleteRange");
        return customerService.deleteRange(customerDtoList);
    }

    @ApiOperation(value = "财务数据客户修改")
    // @ApiImplicitParam(name = "customerDtoList", value = "customerDto List",
    // required = true, dataType = "List<CustomerDto>")
    @RequestMapping(value = "/UpdateRange", method = RequestMethod.POST)
90 91
    public @ResponseBody
    List<OperationResultDto<CustomerDto>> updateRange(
eddie.woo's avatar
eddie.woo committed
92 93 94 95 96 97 98 99 100
            @RequestBody List<CustomerDto> customerDtoList) {
        logger.debug("enter updateRange");
        return customerService.updateRange(customerDtoList);
    }

    @ApiOperation(value = "批量上传账套下客户列表")
    // @ApiImplicitParam(name = "file", value = "file", required = true, dataType =
    // "CommonsMultipartFile")
    @RequestMapping(value = "/Upload", method = RequestMethod.POST)
101 102
    public @ResponseBody
    Object upload(@RequestParam(value = "file", required = false) CommonsMultipartFile inputFile,
103
                  @RequestParam(value = "enterpriseAccountId", required = false) String enterpriseAccountId,
104
                  @RequestParam(value = "action", required = false) String action) {
eddie.woo's avatar
eddie.woo committed
105
        logger.debug("enter upload");
106
        logger.debug("enterpriseAccountId: {}", enterpriseAccountId);
eddie.woo's avatar
eddie.woo committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
        logger.debug("action: {}", action);

        if (inputFile == null || inputFile.getSize() <= 0) {
            OperationResultDto<?> operationResultDto = new OperationResultDto<>();
            operationResultDto.setResult(false);
            operationResultDto.setResultMsg("NoFile");
            logger.warn("File is empty");
            return operationResultDto;
        }
        logger.debug("file name: " + inputFile.getOriginalFilename());
        InputStream input = null;
        try {
            input = inputFile.getInputStream();
        } catch (IOException e) {
            throw Lang.wrapThrow(e);
        }
123
        return customerService.upload(input, inputFile.getOriginalFilename(), action, enterpriseAccountId);
eddie.woo's avatar
eddie.woo committed
124
    }
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258

    @RequestMapping(value = "GetCustomsInvoiceDataForDisplay", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseEntity GetCustomsInvoiceDataForDisplay(@RequestParam Integer fromPeriod, @RequestParam Integer toPeriod,
                                                          @RequestParam String criteria, @RequestParam String pagination) {
        CustomsInvoiceFilter filter = new CustomsInvoiceFilter();
        if (!StringUtils.isEmpty(criteria) && criteria != "null")
            filter = JSON.parseObject(criteria, CustomsInvoiceFilter.class);
        else
            filter = null;

        // PAGINATION INFORMATION:
        PagingDto paging = new PagingDto();
        if (!StringUtils.isEmpty(pagination) && pagination != "null") {
            paging = JSON.parseObject(pagination, PagingDto.class);
        } else
            paging = null;

        String passResponse = ConvertCustomsInvoiceDataToJson(fromPeriod, toPeriod, filter, paging);
        return ResponseEntity.ok(passResponse);
    }

    private String ConvertCustomsInvoiceDataToJson(int fromPeriod, int toPeriod, CustomsInvoiceFilter filter, PagingDto pagination) {
        String result = "";
        List<CustomsInvoiceDto> balanceList = customerService.GetCustomsInvoicesByPeriodIds(fromPeriod, toPeriod);

        if (filter != null && !balanceList.isEmpty())
            balanceList = FilterCustomsInvoiceDataList(filter, balanceList);

        if (pagination != null) {
            int pageAskingFor = 0;

            if (pagination.getPageIndex() > 0)
                pageAskingFor = pagination.getPageIndex() - 1;
            // Update Total Counts:
            pagination.setTotalCount(balanceList.size());
            balanceList = balanceList.subList(pageAskingFor * pagination.getPageSize(), pageAskingFor * pagination.getPageSize() + pagination.getPageSize());
        }

        List<JsonCustomsInvoice> recordList = new ArrayList<>();
        JsonCustomsInvoice record;

        int indexNumber = 1;
        for (CustomsInvoiceDto item : balanceList) {
            record = new JsonCustomsInvoice();
            record.RecordNumber = indexNumber + "";
            record.CustomsId = item.getCustomsId();
            record.PayNum = item.getPayNum();
            record.IssueDate = new SimpleDateFormat("yyyy-MM-dd").format(item.getIssueDate());
            record.InvoiceTaxAmount = item.getInvoiceTaxAmount() != null ? item.getInvoiceTaxAmount().toString() : "0.00";
            record.InvoiceAmount = item.getInvoiceAmount() != null ? item.getInvoiceAmount().toString() : "0.00";
            record.AuditResult = item.getAuditResult();
            record.PeriodId = item.getPeriodId();
            recordList.add(record);
            indexNumber++;
        }
        PagingResultDto<JsonCustomsInvoice> listWithPaging = new PagingResultDto<>();
        listWithPaging.setPageInfo(pagination);
        listWithPaging.setList(recordList);

        result = JSON.toJSONString(listWithPaging);
        return result;
    }

    private List<CustomsInvoiceDto> FilterCustomsInvoiceDataList(CustomsInvoiceFilter filter, List<CustomsInvoiceDto> dataList) {
        // Do filters:
        if (!dataList.isEmpty()) {
            // period:
            //if (filter.getPeriodFrom ()!=null&& filter.PeriodFrom.Value > 0)
            //    dataList = dataList.stream().filter((-=> x.PeriodId.Value == filter.getPeriodFrom().doubleValue().ToList();


            // payment number  -- Added Like operation

            if (!StringUtils.isEmpty(filter.getPayNum())) {
                String[] names = filter.getPayNum().split(" ");
                List<CustomsInvoiceDto> ds = new ArrayList<>();
                for (String name : names) {
                    ds.addAll(dataList.stream().filter(x -> x.getPayNum().contains(name)).collect(Collectors.toList()));
                }
                dataList = ds;
            }

            // tax amount
            if (filter.getTaxAmountFrom() != null || filter.getTaxAmountTo() != null) {
                //both start and end dates are there
                if (filter.getTaxAmountFrom() != null && filter.getTaxAmountTo() != null)
                    dataList = dataList.stream().filter(x -> x.getInvoiceAmount() != null && (filter.getTaxAmountFrom().doubleValue()
                            <= x.getInvoiceTaxAmount().doubleValue() && filter.getTaxAmountTo().doubleValue() >= x.getInvoiceTaxAmount().doubleValue()))
                            .collect(Collectors.toList());
                else if (filter.getTaxAmountFrom() != null)
                    dataList = dataList.stream().filter(x -> x.getInvoiceTaxAmount() != null && filter.getTaxAmountFrom()
                            .doubleValue() <= x.getInvoiceTaxAmount().doubleValue()).collect(Collectors.toList());
                else
                    dataList = dataList.stream().filter(x -> x.getInvoiceTaxAmount() != null && filter.getTaxAmountTo()
                            .doubleValue() >= x.getInvoiceTaxAmount().doubleValue()).collect(Collectors.toList());
            }

            // amount

            if (filter.getInvoiceAmountFrom() != null || filter.getInvoiceAmountTo() != null) {
                //both start and end dates are there
                if (filter.getInvoiceAmountFrom() != null && filter.getInvoiceAmountTo() != null)
                    dataList = dataList.stream().filter(x -> x.getInvoiceAmount() != null && (filter.getInvoiceAmountFrom().doubleValue()
                            <= x.getInvoiceAmount().doubleValue() && filter.getInvoiceAmountTo().doubleValue()
                            >= x.getInvoiceAmount().doubleValue())).collect(Collectors.toList());
                else if (filter.getInvoiceAmountFrom() != null)
                    dataList = dataList.stream().filter(x -> x.getInvoiceAmount() != null && filter.getInvoiceAmountFrom().doubleValue()
                            <= x.getInvoiceAmount().doubleValue()).collect(Collectors.toList());
                else
                    dataList = dataList.stream().filter(x -> x.getInvoiceAmount() != null && filter.getInvoiceAmountTo().doubleValue()
                            >= x.getInvoiceAmount().doubleValue()).collect(Collectors.toList());
            }


            // invoice start date and/or invoice end date
            if (filter.getInvoiceStartDate() != null || filter.getInvoiceEndDate() != null) {
                //both start and end dates are there
                if (filter.getInvoiceStartDate() != null && filter.getInvoiceEndDate() != null)
                    dataList = dataList.stream().filter(x -> x.getIssueDate() != null && (filter.getInvoiceStartDate()
                            .before(x.getIssueDate()) && filter.getInvoiceEndDate().after(x.getIssueDate()))).collect(Collectors.toList());
                else if (filter.getInvoiceStartDate() != null)
                    dataList = dataList.stream().filter(x -> x.getIssueDate() != null && filter.getInvoiceStartDate()
                            .before(x.getIssueDate())).collect(Collectors.toList());
                else
                    dataList = dataList.stream().filter(x -> x.getIssueDate() != null && filter.getInvoiceEndDate()
                            .after(x.getIssueDate())).collect(Collectors.toList());
            }

        }

        return dataList;
    }


eddie.woo's avatar
eddie.woo committed
259
}