package cn.com.victorysoft.business.condition.controller;

import cn.com.victorysoft.business.condition.entity.FlushDaily;
import cn.com.victorysoft.business.condition.param.FlushDailyExportParam;
import cn.com.victorysoft.business.condition.param.FlushDailyParam;
import cn.com.victorysoft.business.condition.service.FlushDailyService;
import cn.com.victorysoft.business.sys.entity.ExportResult;
import cn.com.victorysoft.business.util.DateUtils;
import cn.com.victorysoft.business.util.ExcelUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.converters.BigDecimalConverter;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Api(tags = "洗井日报")
@RestController
@RequestMapping("condition/flushDaily")
public class FlushDailyController {
    @Resource
    private FlushDailyService flushDailyService;

    @GetMapping("/page")
    @ApiOperation("查询分页数据")
    public PageInfo<FlushDaily> Page(FlushDailyParam param) {
        return flushDailyService.selectPage(param);
    }

    @PostMapping("/export")
    @ApiOperation("导出数据")
    public ExportResult selectListExport(@RequestBody FlushDailyExportParam param) {
        List<FlushDaily> list = flushDailyService.selectList(param);

        if (param.getAddress() != null){
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setNum(new BigDecimal(i + 1));
                list.get(i).setNy(DateUtils.format(list.get(i).getRecordDate(), "MM"));
                if (list.get(i).getAfterPt() == null) {
                    list.get(i).setAfterPt(new BigDecimal(0));
                }
                if (list.get(i).getBeforePt() == null) {
                    list.get(i).setBeforePt(new BigDecimal(0));
                }
                list.get(i).setComparePt(list.get(i).getAfterPt().subtract(list.get(i).getBeforePt()));
                if (list.get(i).getAfterPlanWater() == null) {
                    list.get(i).setAfterPlanWater(new BigDecimal(0));
                }
                if (list.get(i).getBeforePlanWater() == null) {
                    list.get(i).setBeforePlanWater(new BigDecimal(0));
                }
                list.get(i).setComparePlanWater(list.get(i).getAfterPlanWater().subtract(list.get(i).getBeforePlanWater()));
                if (list.get(i).getAfterWater() == null) {
                    list.get(i).setAfterWater(new BigDecimal(0));
                }
                if (list.get(i).getBeforeWater() == null) {
                    list.get(i).setBeforeWater(new BigDecimal(0));
                }
                list.get(i).setCompareWater(list.get(i).getAfterWater().subtract(list.get(i).getBeforeWater()));
            }
        }else {

        }
        return ExcelUtils.export(param.getColumnConfig(), list, param.getFileName());
    }

    @PutMapping("/update")
    @ApiOperation("更新")
    public int update(@RequestBody FlushDaily param) {
        return flushDailyService.update(param);
    }

}
