package com.zy.asrs.controller; import com.alibaba.excel.EasyExcel; import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.plugins.Page; import com.core.annotations.ManagerAuth; import com.core.common.Cools; import com.core.common.DateUtils; import com.core.common.R; import com.core.common.SnowflakeIdWorker; import com.core.exception.CoolException; import com.zy.asrs.entity.Review; import com.zy.asrs.entity.ReviewDetl; import com.zy.asrs.importexcle.ImportReviewDto; import com.zy.asrs.importexcle.ImportReviewListener; import com.zy.asrs.mapper.ReviewDetlMapper; import com.zy.asrs.service.ReviewDetlService; import com.zy.asrs.service.ReviewService; import com.zy.common.web.BaseController; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URLEncoder; import java.util.*; @RestController public class ReviewController extends BaseController { @Autowired private ReviewService reviewService; @Autowired private ReviewDetlService reviewDetlService; @Autowired private SnowflakeIdWorker snowflakeIdWorker; @Autowired private PlatformTransactionManager transactionManager; @Resource private ReviewDetlMapper reviewDetlMapper; @Resource private HttpServletResponse response; @RequestMapping("/exportReview") public synchronized void exportReview(String ids) throws IOException { if (Cools.isEmpty(ids)) { return; } String[] idsArr = ids.split(","); Set set = new HashSet(); for (String id : idsArr) { if (!Cools.isEmpty(id)) { set.add(Long.parseLong(id)); } } List reviewDetls = reviewDetlService.selectList(new EntityWrapper().in("order_id", set)); response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8"); String fileName = URLEncoder.encode("单据复核明细表", "UTF-8"); response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx"); EasyExcel.write(response.getOutputStream(), ReviewDetl.class) .registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()) .sheet("表1") .doWrite(reviewDetls); } @PostMapping("/importReview") @ManagerAuth(memo = "单据导入") @ApiOperation(value = "单据导入", produces = MediaType.APPLICATION_JSON_VALUE) public R importOrderData(@RequestParam("file") MultipartFile multipartFile) { try { importOrder(multipartFile); } catch (Exception e) { e.printStackTrace(); return R.error(e.getMessage()); } return R.ok("导入成功"); } @Transactional(rollbackFor = Exception.class) public void importOrder(MultipartFile multipartFile) throws IOException { // 考核数据的判重使用order_id,check_type的组合唯一索引解决 EasyExcel.read(multipartFile.getInputStream(), ImportReviewDto.class, new ImportReviewListener(transactionManager, reviewService, reviewDetlService, snowflakeIdWorker, getUserId())).sheet().doReadSync(); } @RequestMapping(value = "/review/head/page/auth") @ManagerAuth public R head(@RequestParam(defaultValue = "1") Integer curr, @RequestParam(defaultValue = "10") Integer limit, @RequestParam(required = false) String orderByField, @RequestParam(required = false) String orderByType, @RequestParam Map param) { EntityWrapper wrapper = new EntityWrapper<>(); excludeTrash(param); convert(param, wrapper); if (!Cools.isEmpty(orderByField)) { wrapper.orderBy(humpToLine(orderByField), "asc".equals(orderByType)); } else { wrapper.orderBy("settle").orderBy("create_time", false); } wrapper.eq("status", 1); return R.ok(reviewService.selectPage(new Page<>(curr, limit), wrapper)); } private void convert(Map map, EntityWrapper wrapper) { for (Map.Entry entry : map.entrySet()) { String val = String.valueOf(entry.getValue()); if (val.contains(RANGE_TIME_LINK)) { String[] dates = val.split(RANGE_TIME_LINK); wrapper.ge(entry.getKey(), DateUtils.convert(dates[0])); wrapper.le(entry.getKey(), DateUtils.convert(dates[1])); } else { wrapper.like(entry.getKey(), val); } } } @RequestMapping(value = "/review/update/auth") @ManagerAuth public R update(Review review) { if (Cools.isEmpty(review) || null == review.getId()) { return R.error(); } Review review2 = reviewService.selectById(review.getId()); review2.setSettle(review.getSettle()); review.setUpdateBy(getUserId()); review.setUpdateTime(new Date()); if (!reviewService.updateById(review)) { throw new CoolException("修改失败"); } return R.ok(); } @RequestMapping(value = "/review/delete/auth") @ManagerAuth(memo = "手动删除订单") @Transactional public R delete(@RequestParam Long orderId) { Review review = reviewService.selectById(orderId); reviewDetlService.delete(new EntityWrapper().eq("order_id", orderId)); reviewService.deleteById(orderId); return R.ok(); } }