package com.jpsoft.shinestar.modules.base.controller;

import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.base.entity.CompanyGoodInfo;
import com.jpsoft.shinestar.modules.base.service.CompanyGoodInfoService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/companyGoodType")
public class CompanyGoodTypeController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private CompanyGoodInfoService companyGoodInfoService;

    @ApiOperation(value="添加信息")
    @PostMapping("add")
    public MessageResult<CompanyGoodInfo> add(@ModelAttribute CompanyGoodInfo companyGoodInfo){
        MessageResult<CompanyGoodInfo> msgResult = new MessageResult<>();

        try {
            companyGoodInfo.setId(UUID.randomUUID().toString());

            int affectCount = companyGoodInfoService.insert(companyGoodInfo);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(companyGoodInfo);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库添加失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="获取信息")
    @GetMapping("detail/{id}")
    public MessageResult<CompanyGoodInfo> detail(@PathVariable("id") String id){
        MessageResult<CompanyGoodInfo> msgResult = new MessageResult<>();

        try {
            CompanyGoodInfo companyGoodInfo = companyGoodInfoService.get(id);

            if (companyGoodInfo != null) {
                msgResult.setResult(true);
                msgResult.setData(companyGoodInfo);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库不存在该记录!");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="更新用户")
    @PostMapping("update")
    public MessageResult<CompanyGoodInfo> update(@ModelAttribute CompanyGoodInfo companyGoodInfo){
        MessageResult<CompanyGoodInfo> msgResult = new MessageResult<>();

        try {
            int affectCount = companyGoodInfoService.update(companyGoodInfo);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(companyGoodInfo);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库更新失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="删除用户")
    @PostMapping("delete/{id}")
    public MessageResult<CompanyGoodInfo> delete(@PathVariable("id") String id){
        MessageResult<CompanyGoodInfo> msgResult = new MessageResult<>();

        try {
            int affectCount = companyGoodInfoService.delete(id);

            if (affectCount > 0) {
                msgResult.setResult(true);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库删除失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }


    @ApiOperation(value="批量删除")
    @PostMapping("batchDelete/{id}")
    public MessageResult<CompanyGoodInfo> batchDelete(@PathVariable("id") String id){
        MessageResult<CompanyGoodInfo> msgResult = new MessageResult<>();

        msgResult.setResult(false);
        msgResult.setMessage("未实现");

        return msgResult;
    }

    @ApiOperation(value="列表")
    @RequestMapping(value = "list",method = RequestMethod.POST)
    public MessageResult<Map> list(
            String id,
            @RequestParam(value="draw",defaultValue="1") int draw,
            @RequestParam(value="start",defaultValue="1") int start,
            @RequestParam(value="length",defaultValue="20") int length,
            HttpServletRequest request){
        String subject = (String)request.getAttribute("subject");

        //当前用户ID
        System.out.println(subject);

        MessageResult<Map> msgResult = new MessageResult<>();

        Map<String,Object> searchParms = new HashMap<>();

        List<Sort> sortList = new ArrayList<>();
        sortList.add(new Sort("id_","asc"));

        if (StringUtils.isNotEmpty(id)) {
            searchParms.put("id","%" + id + "%");
        }

        int pageNum = start / length;

        if (start % length != 0) {
            pageNum++;
        }

        int pageSize = length;

        Page<CompanyGoodInfo> page = companyGoodInfoService.pageSearch(searchParms,pageNum,pageSize,sortList);

        HashMap<String, Object> wrapper = new HashMap<String, Object>();
        wrapper.put("draw",draw);
        wrapper.put("recordsTotal", page.getTotal());
        wrapper.put("recordsFiltered", page.getTotal());
        wrapper.put("data", page.getResult());

        msgResult.setResult(true);
        msgResult.setData(wrapper);

        return msgResult;
    }
}
