package xiaoqu.home.open.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
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.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import xiaoqu.home.open.controller.export.AttendanceRecordExport;
import xiaoqu.home.open.controller.input.AttendanceRecordPageInput;
import xiaoqu.home.open.controller.output.AttendanceRecordOutput;
import xiaoqu.home.open.model.db1.User;
import xiaoqu.home.open.model.db2.AttendanceRecord;
import xiaoqu.home.open.model.db2.FieldWebpageAuthority;
import xiaoqu.home.open.publicBean.BaseOutput;
import xiaoqu.home.open.publicBean.PageOutput;
import xiaoqu.home.open.service.db2.AttendanceRecordService;
import xiaoqu.home.open.service.db2.FieldWebpageAuthorityService;
import xiaoqu.home.open.utils.ResultResponseBeanUtil;
import xiaoqu.home.open.utils.StringUtil;
import xiaoqu.home.open.utils.UserUtils;
import xiaoqu.home.open.utils.WrapperUtils;
import xiaoqu.home.open.utils.poi.ExcelUtil;

@Controller
@RequestMapping(value = "/attendance")
@Api(value = "考勤打卡联调接口", description = "考勤打卡联调接口")
public class AttendanceController {
	@Autowired
	private AttendanceRecordService attendanceRecordService;
	@Autowired
	private FieldWebpageAuthorityService fieldWebpageAuthorityService;

	@ApiOperation(value = "分页查询")
	@ResponseBody
	@RequestMapping(value = "/pageList", method = RequestMethod.POST)
	public BaseOutput<PageOutput<AttendanceRecordOutput>> pageList(HttpServletRequest request, @RequestBody AttendanceRecordPageInput input) {
		PageOutput<AttendanceRecordOutput> output = new PageOutput<AttendanceRecordOutput>();
		Long districtId = input.getDistrictId();
		if (StringUtil.isNull(districtId)) {
			return ResultResponseBeanUtil.fail("请选择站点");
		}
		QueryWrapper<AttendanceRecord> wrapper = WrapperUtils.getWrapper(false);
		wrapper = WrapperUtils.beanToWrapper(wrapper, input);
		User user = UserUtils.getUser(request);
		List<FieldWebpageAuthority> fieldList = fieldWebpageAuthorityService.getFieldList(input.getWebId(), user.getPropertyAuthorityId());
		String[] columns = WrapperUtils.entityToColumns(fieldList);
		wrapper.select(columns);
		Page<AttendanceRecord> pageList = new Page<AttendanceRecord>(input.getPage(), input.getRows());
		List<AttendanceRecord> entityList = attendanceRecordService.page(pageList, wrapper).getRecords();
		List<AttendanceRecordOutput> outList = new ArrayList<AttendanceRecordOutput>();
		for (AttendanceRecord bean : entityList) {
			AttendanceRecordOutput outputBean = new AttendanceRecordOutput();
			BeanUtils.copyProperties(bean, outputBean);
			outList.add(outputBean);
		}
		output.setPage(pageList.getCurrent());
		output.setRecords(pageList.getTotal());
		output.setTotal(pageList.getPages());
		output.setRows(outList);
		return ResultResponseBeanUtil.success(output);
	}
	@ApiOperation(value = "导出")
	@PostMapping("/export")
	public void export(HttpServletRequest request, HttpServletResponse response, @RequestBody AttendanceRecordPageInput input) throws IOException {
		Long districtId = input.getDistrictId();
		if (districtId == null || districtId == 0) {
			return;
		}
		List<AttendanceRecordExport> outList = new ArrayList<AttendanceRecordExport>();
		User user = UserUtils.getUser(request);
		QueryWrapper<AttendanceRecord> wrapper = WrapperUtils.getWrapper(false);
		wrapper = WrapperUtils.beanToWrapper(wrapper, input);
		List<AttendanceRecord> entityList = attendanceRecordService.list(wrapper);
		for (AttendanceRecord bean : entityList) {
			AttendanceRecordExport outputBean = new AttendanceRecordExport();
			BeanUtils.copyProperties(bean, outputBean);
			outList.add(outputBean);
		}
		List<FieldWebpageAuthority> fieldList = fieldWebpageAuthorityService.getExportFieldList(input.getWebId(), user.getAuthorityId());
		String[] exportColumns = WrapperUtils.entityToColumns(fieldList);
		ExcelUtil<AttendanceRecordExport> util = new ExcelUtil<AttendanceRecordExport>(AttendanceRecordExport.class);
		util.exportExcelByColumns(response, outList, "打卡记录", exportColumns);
	}
}
