package com.sy.coinage.workshop.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.List;
import javax.validation.Valid;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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.RestController;

import com.sy.coinage.core.annotation.ActiveFastJsonProfile;
import com.sy.coinage.core.annotation.CurrentUser;
import com.sy.coinage.core.aop.annotation.Permission;
import com.sy.coinage.core.aop.annotation.Operation;
import com.sy.coinage.core.controller.BaseController;
import com.sy.coinage.core.interceptor.Create;
import com.sy.coinage.core.interceptor.Update;
import com.sy.coinage.core.req.*;
import com.sy.coinage.core.vo.*;

import com.sy.coinage.workshop.model.*;
import com.sy.coinage.workshop.service.IDeliveryPlanDetailService;
import com.sy.coinage.workshop.vo.*;
import com.sy.coinage.workshop.req.*;

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

@Api(value = "接口", tags = {"接口"})
@RestController
@RequestMapping(value = "/deliveryPlanDetail")
public class DeliveryPlanDetailController extends BaseController {

    @Autowired
    private IDeliveryPlanDetailService deliveryPlanDetailService;

    @ApiOperation(value = "列表")
    @PostMapping("/list")
    @ActiveFastJsonProfile(groups = "list", clazz = DeliveryPlanDetail.class)
    public List<DeliveryPlanDetail> list(@RequestBody @ApiParam(value = "列表查询请求bean") DeliveryPlanDetailListReq req, @CurrentUser CacheUserVO user) {
        QueryWrapper<DeliveryPlanDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("plan_id", req.getPlanId());
        queryWrapper.orderByAsc("serial_number");
        List<DeliveryPlanDetail> data = this.deliveryPlanDetailService.list(queryWrapper);
        return data;
    }

    @ApiOperation(value = "获取详情")
    @PostMapping("/get")
    @ActiveFastJsonProfile(groups = "get", clazz = DeliveryPlanDetail.class)
    public DeliveryPlanDetailVO get(@RequestBody @ApiParam(value = "主键请求bean") @Valid IdReq req) {
        DeliveryPlanDetailVO data = null;
        DeliveryPlanDetail model = this.deliveryPlanDetailService.getById(req.getId());
        data = new DeliveryPlanDetailVO();
        BeanUtils.copyProperties(model, data);
        return data;
    }

    // @ApiOperation(value = "选项")
    // @GetMapping("/opt")
    // public List<OptVO> opt(){
    // List<OptVO> data = this.deliveryPlanDetailService.opt();
    //   return data;
    // }

    @ApiOperation(value = "新增")
    @Operation(value = "新增")
    @PostMapping("/add")
    @Permission("deliveryPlanDetail:add")
    public Object add(@RequestBody @ApiParam(value = "新增请求bean") @Validated({Create.class}) DeliveryPlanDetailReq req, @CurrentUser CacheUserVO user) {
        //  this.deliveryPlanDetailService.add(req, user);
        DeliveryPlanDetail model = new DeliveryPlanDetail();
        BeanUtils.copyProperties(req, model);
        //    model.setTenantId(user.getTenantId());
        //    model.setCreateBy(user.getName());
        //    model.setUpdateBy(user.getName());
        this.deliveryPlanDetailService.save(model);
        return "";
    }

    @ApiOperation(value = "修改")
    @Operation(value = "修改")
    @PostMapping("/update")
    @Permission("deliveryPlanDetail:update")
    public Object update(@RequestBody @ApiParam(value = "修改请求bean") @Validated({Update.class}) DeliveryPlanDetailReq req, @CurrentUser CacheUserVO user) {
        //  this.deliveryPlanDetailService.update(req, user)
        DeliveryPlanDetail model = this.deliveryPlanDetailService.getById(req.getId());
        // 验证是否相同租户
//        this.validTenant(user, model.getTenantId());
        BeanUtils.copyProperties(req, model);
        //    model.setUpdateBy(user.getName());
        this.deliveryPlanDetailService.updateById(model);
        return "";
    }

    //  @ApiOperation(value = "修改状态")
    //  @Operation(value = "修改状态")
    //  @PostMapping("/status")
    //  @Permission("deliveryPlanDetail:update")
    //  public Integer status(@RequestBody @Valid IdReq req, @CurrentUser CacheUserVO user){
    //    DeliveryPlanDetail model = this.deliveryPlanDetailService.getById(req.getId());
    // 验证是否相同租户
    //    this.validTenant(user, model.getTenantId());
    //    if(null != model.getStatus() && model.getStatus().intValue() == Constants.STATUS_FORBIDDEN) {
    //      model.setStatus(Constants.STATUS_USABLE);
    //    } else {
    //      model.setStatus(Constants.STATUS_FORBIDDEN);
    //    }
    //    this.deliveryPlanDetailService.updateById(model);
    //    return model.getStatus();
    //  }


    @ApiOperation(value = "删除")
    @Operation(value = "删除")
    @PostMapping("/delete")
    @Permission("deliveryPlanDetail:delete")
    public Object delete(@RequestBody @ApiParam(value = "主键请求bean") @Valid IdReq req, @CurrentUser CacheUserVO user) {
        DeliveryPlanDetail model = this.deliveryPlanDetailService.getById(req.getId());
        // 验证是否相同租户
//        this.validTenant(user, model.getTenantId());
        //    model.setDelFlag(Constants.STATUS_FORBIDDEN);
        //    this.deliveryPlanDetailService.updateById(model);
        this.deliveryPlanDetailService.removeById(req.getId());
        return "";
    }

    //  private static String[] codes = {"id", "planId", "rfid", "serialNumber", "crownBetweenStr", "type", "qty", "tareWeight", "grossWeight", "weightorId", "weightor", "printTime", "crownFrom", "crownTo"};
    //  private static String[] names = {"id", "发货计划ID", " rfid:冗余字段:分配计划时，还没有rfid.", "编号", "冠字号号段字符串（eg:佰21伍003-0921-佰21伍003-0980）", "拍子类型1:整拍 2: 前  3:后(由拍子类型、数量决定拍子取的箱子)", "箱子数量", "皮重", "毛重", "称重人ID", "称重人", "打印时间", "冠字号起始号（eg:003-2121）", "冠字号结束号(eg:003-2121）"};

    //  @Autowired
    //  private CustomConfig customConfig;

//    @ApiOperation(value = "导出")
//    @PostMapping("/exp")
//    public Object exp(@RequestBody IdsListReq req){
//        FileVO data = new FileVO();
//      LambdaQueryWrapper<DeliveryPlanDetail> queryWrapper = new LambdaQueryWrapper<>();
//        List<DeliveryPlanDetail> list = this.deliveryPlanDetailService.list(queryWrapper);
//        ExcelExportUtil<DeliveryPlanDetail> excelExport = new ExcelExportUtil<>();
//        String fileName = ".xlsx";
//        data.setName(fileName);
//        FileVO resp = excelExport.exportData(names, codes, list, fileName, customConfig.getAttaPath(), customConfig.getAttaUrl());
//        data.setUrl(resp);
//        return data;
//    }

    //  @ApiOperation(value = "导入")
    //  @PostMapping("/imp")
    //  public Object imp(@RequestParam(required=true) MultipartFile file, @CurrentUser CacheUserVO user){
    //      Workbook workbook = ExcelUtil.readExcel(file);
    //      List<DeliveryPlanDetailAddReq> list = ExcelImport.convertToList(DeliveryPlanDetailAddReq.class, workbook, codes, names, 1, 1);
    //      this.validData(list);
    //      this.deliveryPlanDetail.batchAdd(list, user);
    //      return "";
    //  }

    /**
     * 验证数据长度,非空
     */
    //	private void validData(List<DeliveryPlanDetailAddReq> list) {
    //		if(null != list && list.size() > 0) {
    //			for (int i = 0; i < list.size(); i++) {
    //				ValidatorUtils.validateEntity(i, list.get(i));
    //			}
    //		}
    //	}

}
