package io.bdty.modules.cniyv.controller;

import io.bdty.common.annotation.LogOperation;
import io.bdty.common.constant.Constant;
import io.bdty.common.page.PageData;
import io.bdty.common.utils.ExcelUtils;
import io.bdty.common.utils.Result;
import io.bdty.common.validator.AssertUtils;
import io.bdty.common.validator.ValidatorUtils;
import io.bdty.common.validator.group.AddGroup;
import io.bdty.common.validator.group.DefaultGroup;
import io.bdty.common.validator.group.UpdateGroup;
import io.bdty.modules.cniyv.dto.CniyvProductDTO;
import io.bdty.modules.cniyv.dto.CniyvProductTypeDTO;
import io.bdty.modules.cniyv.excel.CniyvProductExcel;
import io.bdty.modules.cniyv.service.CniyvProductService;
import io.bdty.modules.cniyv.service.CniyvProductTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.Map;


/**
 * 产品
 *
 * @author zxj zxj
 * @since 1.0.0 2024-03-12
 */
@RestController
@RequestMapping("cniyv/cniyvproduct")
@Api(tags="产品")
public class CniyvProductController {
    @Autowired
    private CniyvProductService cniyvProductService;
    @Autowired
    private CniyvProductTypeService cniyvProductTypeService;
    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    public Result<PageData<CniyvProductDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<CniyvProductDTO> page = cniyvProductService.page(params);

        for (CniyvProductDTO product : page.getList()) {
        	if (product.getTypeid()!=null) {
        		CniyvProductTypeDTO typeDTO = cniyvProductTypeService.get(product.getTypeid());
            	product.setType(typeDTO!=null?typeDTO.getName():"");
			}
			
		}
        return new Result<PageData<CniyvProductDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("cniyv:cniyvproduct:info")
    public Result<CniyvProductDTO> get(@PathVariable("id") Long id){
        CniyvProductDTO data = cniyvProductService.get(id);
        if (data.getTypeid()!=null) {
    		CniyvProductTypeDTO typeDTO = cniyvProductTypeService.get(data.getTypeid());
    		data.setType(typeDTO!=null?typeDTO.getName():"");
		}
        return new Result<CniyvProductDTO>().ok(data);
    }
    
    @GetMapping("info/{id}")
    @ApiOperation("详情")
    public Result<CniyvProductDTO> info(@PathVariable("id") Long id){
        CniyvProductDTO data = cniyvProductService.get(id);
        if (data.getTypeid()!=null) {
    		CniyvProductTypeDTO typeDTO = cniyvProductTypeService.get(data.getTypeid());
    		data.setType(typeDTO!=null?typeDTO.getName():"");
		}
        return new Result<CniyvProductDTO>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("cniyv:cniyvproduct:save")
    public Result save(@RequestBody CniyvProductDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
		try {
	        String content = URLDecoder.decode(dto.getContent(), "UTF-8");
	        dto.setContent(content);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        cniyvProductService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    @RequiresPermissions("cniyv:cniyvproduct:update")
    public Result update(@RequestBody CniyvProductDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        try {
	        String content = URLDecoder.decode(dto.getContent(), "UTF-8");
	        dto.setContent(content);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        cniyvProductService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("cniyv:cniyvproduct:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        cniyvProductService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("cniyv:cniyvproduct:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<CniyvProductDTO> list = cniyvProductService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "产品", list, CniyvProductExcel.class);
    }
    
    @GetMapping("list")
    @ApiOperation("列表")
    public Result<List<CniyvProductDTO>> list(@ApiIgnore @RequestParam Map<String, Object> params) {
        List<CniyvProductDTO> list = cniyvProductService.list(params);
        return new Result<List<CniyvProductDTO>>().ok(list);
    }

    
}